We are starting a new series of posts aimed at learning how to interact with different programs through their API from a NET6 application written in C#.
In the field of IoT, it is often necessary to combine or access different programs within a device that initiates the 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 to a database, or send it by email.
This integration capability is one of the features that has traditionally been strongest in the Windows operating system. For this purpose, 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 it is the best possible option. In fact, in general, it should be the last option to choose. In many cases, it can be avoided by approaching the project differently. However, it is often a project requirement.
In any case, in this series we will see how to work with different programs through their API, from a NET6 application written in C#. We will start with Microsoft Word, well-known to everyone.
Logically, as it is a Microsoft application, as we will see the integration with .NET is very good. Furthermore, the structure and API that Microsoft offers for its applications, like Word, is excellent.
How to Connect to Microsoft Word with a NET6 Application
For this example, first 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 dropdown menu. Here we choose “Add References” and select “COM Libraries” in the “COM Components” tab.
Add “Microsoft Word xx.0 Object Library” to the list of components, where xx represents the version of Microsoft Word you have installed on your computer.
Then, 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
It’s that simple. In the next post in the series, we will see how to do something similar using Microsoft Excel.
Download the Code
All the code for this post is available for download on Github.

