We start a new series of entries aimed at seeing how to interact with different programs through their API from an application in NET6 written in C#.
Within the field of IoT, it is often necessary to combine or access different programs within a gadget that initiates communication. For example, we could place a sensor connected to an ESP32 and, at a certain moment, generate a report in a Word file, print it, save it in a database, or send it by email, for example.
This integration capability is one of the features that has traditionally been strongest in a Windows operating system. For this, there are different mechanisms such as the COM or ActiveX object model or more recently the .NET environment.
Being able to interact with a program through its API does not mean that it is the best possible option. In fact, in general, it should be the last option to choose. On many occasions, it is avoidable by approaching the project differently. However, in many cases, it is a project requirement.
In any case, in this series we are going to see how to interact with different programs through their API, from a NET6 application written in C#. We are going to start with Microsoft Word, well known by all.
Logically, being an application of Microsoft itself, as we will see the integration with .NET is very good. In addition, the structure and API that Microsoft offers for its applications, such as Word, is excellent.
How to connect to Microsoft Word with a NET6 application
For this example, first of all we will create a simple NET6 console application. Next, we must add references to the Microsoft Word assemblies.
To do this, click on “Tools” and select “References” in the drop-down menu. Here we choose “Add references” and select “COM Component Libraries”, in the “COM Components” tab.
We add “Microsoft Word xx.0 Object Library” to the list of components, where xx represents the version of Microsoft Word installed on your computer.
Next, we copy this code, which is a summarized and corrected version of Microsoft’s own examples.
using Microsoft.Office.Interop;
using System.Reflection;
object missing = Missing.Value;
object endOfDoc = "\\endofdoc";
var applicationWord = new Microsoft.Office.Interop.Word.Application();
applicationWord.Visible = true;
var document = applicationWord.Documents.Add(ref missing, ref missing, ref missing, ref missing);
//Insert a paragraph at the end of the document.
object selectionRange = document.Bookmarks.get_Item(ref endOfDoc).Range;
var paragraph2 = document.Content.Paragraphs.Add(ref selectionRange);
paragraph2.Range.Text = "Heading 2";
paragraph2.Format.SpaceAfter = 6;
paragraph2.Range.InsertParagraphAfter();
//Insert another paragraph.
selectionRange = document.Bookmarks.get_Item(ref endOfDoc).Range;
var paragraph3 = document.Content.Paragraphs.Add(ref selectionRange);
paragraph3.Range.Text = "This is a sentence of normal text. Now here is a table:";
paragraph3.Range.Font.Bold = 0;
paragraph3.Format.SpaceAfter = 24;
paragraph3.Range.InsertParagraphAfter();
//Insert a 3 x 5 table, fill it with data, and make the first row bold and italic.
var wordRange = document.Bookmarks.get_Item(ref endOfDoc).Range;
var table = document.Tables.Add(wordRange, 3, 5, ref missing, ref missing);
table.Range.ParagraphFormat.SpaceAfter = 6;
int row, column;
string strText;
for (row = 1; row <= 3; row++)
for (column = 1; column <= 5; column++)
{
strText = "r" + row + "c" + column;
table.Cell(row, column).Range.Text = strText;
}
table.Rows[1].Range.Font.Bold = 1;
table.Rows[1].Range.Font.Italic = 1;
Console.ReadLine();
With this code, we have generated a new Word document, added a couple of paragraphs and a table. Nothing difficult. You can find more information about the Microsoft Word API in NET at this link https://learn.microsoft.com/es-es/previous-versions/office/troubleshoot/office-developer/automate-word-create-file-using-visual-c
That’s how simple it is. In the next entry in the series, we will see how to do something similar using Microsoft Excel.
Download the code
All the code from this post is available for download on Github.