Simulating Human Input to Microsoft Word with C#

How to Program C# toType into a Word Document

© Mark Alexander Bain

Jun 7, 2009
Simulating Human Input to Microsoft Word with C, Mark Alexander Bain
A C# programmer can insert whole paragraphs into a Microsoft Word Document or they can program their application to type information in as a human would do.

It is very easy for the C# programmer to design an application that will automatically create a new Microsoft Word document adding whole paragraphs to it (as shown in How to Create a Microsoft Word Document with C#).

However, that is not how the average human being enters text into Microsoft Word. They, of course, type the text in letter by letter, formatting it as they go along and producing paragraphs that contain (for example) both normal text and text in italics. This behavior may seem complex but is, if fact, very easy to simulate when using C#.

Opening a Blank Microsoft Word with C#

The first step is to create a new Microsoft Word document:

object oMissing = System.Reflection.Missing.Value;
Word.Application oWord = new Word.Application();
Word.Document oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oWord.Visible = true;

This code will open a blank document that is visible to the user and which will be ready to receive any inputs from the C# application.

Obtaining the Word Document Selection with C#

The key to simulating the input form a human user is to use the Word selection. This places any text in the document at the point at which the cursor has been placed. It is obtained by using:

Word.Selection oSelection = oWord.Selection;

This will also place the cursor at the start of the Word document.

Setting the Style of a Word Document Paragraph with C#

In many documents the first paragraph will actually be a heading. This is done by setting the selection's style:

object styleHeading1 = "Heading 1";
oSelection.set_Style(ref styleHeading1);

It's worth noting that the style will only be applied to the current paragraph. When the application creates a new paragraph the style will revert to normal.

Changing the Font of the Word Document Paragraph

As well as changing the style, the font to be used can be changed:

oSelection.Font.Name = "Arial";
oSelection.Font.Size = 14;

It's always worth remembering that this will only affect the text typed after the current cursor location.

Typing Text to a Word Document with C#

With the style and fonts updated appropriately the C# application can type in the required text:

oSelection.TypeText("Creating and Formatting a Microsoft Word Document");

And, since this is a header, the next step is to add a new (normal) paragraph.

Starting a New Paragraph

Each paragraph is ended by using the TypeParagraph method:

oSelection.TypeParagraph();

Any text typed in now will be in a new paragraph.

Creating a Paragraph With a Normal Style

As mentioned before, the applied style will end when a new paragraph is started. The new paragraph will, therefore, use the normal style:

oSelection.Font.Size = 12;
oSelection.TypeText("This document shows how to create a Word Document. ");
oSelection.TypeText("The code simulates the human use of the keyboard.");
oSelection.TypeParagraph();

If a user runs the application at this point then they will see two paragraphs: one using the “Heading 1” style and one using the “Normal” standard.

Changing Fonts Within a Paragraph

One important aspect of the human entered text is that a single paragraph is likely to contain a mixture of fonts. For example it may contain some (but not all) words in italics. The programmer achieves this by typing a portion of text, changing the font, typing the key word and then returning the font to normal:

oSelection.TypeText("This technique allows fonts to change ");
oSelection.Font.Italic = 1;
oSelection.TypeText("within ");
oSelection.Font.Italic = 0;
oSelection.TypeText("a paragraph. ");

Now the user will see a single word (“within”) in italics and all other words in the standard font (as shown in figure 1 as the bottom of this article).

Saving and Closing the New Microsoft Word Document

At the moment the document will be visible to the user but will not have been saved to disk. Therefore, the next stage to to save the data and close the application, and that's covered in How to Create a Microsoft Word Document with C#.


The copyright of the article Simulating Human Input to Microsoft Word with C# in C Programming is owned by Mark Alexander Bain. Permission to republish Simulating Human Input to Microsoft Word with C# in print or online must be granted by the author in writing.


Simulating Human Input to Microsoft Word with C, Mark Alexander Bain
Figure 1: A Microsoft Word Document Created by C#, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo