C# PROGRAMMING

Windows Office Application Interface

Article by:
Date Published:
Last Modified:

You can use C# to manipulate Microsoft Office programs such as Word and Excel. You can open, create, edit and do essentially everything you can do in the programs natively using instead C# (albeit a little longer…) and libraries provided by Microsoft.

A good tutorial on the Word API can be found here in the MSDN library.

Tutorial

Adding the Microsoft Word x.x (14.0 in this case) Object Library to a C# project.
Adding the Microsoft Word x.x (14.0 in this case) Object Library to a C# project.

To create a word application (the first steps no matter what your doing) is to add the Microsoft Word x.x Object Library reference to your C# project. Right click on your project in the solution explorer, click ‘Add Reference’, click the ‘COM’ tab, and then select Microsoft Word x.x Object Library from the list as shown in the picture. Click ‘OK’ to add it to your project. Note that I was using Microsoft Office 2010 and the Microsoft Word 14.0 Object Library when writing this tutorial, so features/processes could be slightly different for other versions.

Now that the reference has been added, add the following use code to make typing easier

1
using Word = Microsoft.Office.Interop.Word;

Now create a new word application object. This is the equivalent to starting up Microsoft Word.

1
Word.Application wordApp = new Word.Application();

You can choose to make it visible to the user by setting the property

1
wordApp.Visible = true;

To create a new document inside the application (usually the second step), call the method

1
Word.Document wordDoc = wordApp.Documents.Add();

At this point you can start playing around with the document by adding text, pictures, objects (such as textboxes, rectangles, e.t.c). See below for some hints. All hints assume you have instantiated the Word application and at least one document inside it.

Text Boxes:

To create a textbox:

1
Word.Shape textBox = wordDoc.Shapes.AddTextbox(...);

To change weather or not you can see the text box border modify the property:

1
textBox.Line.Visible

To insert text into the textbox modify the property:

1
textBox.TextFrame.TextRange.Text

To change the font size:

1
textBox.TextFrame.TextRange.Font.Size

To change the font colour:

1
textBox.TextFrame.TextRange.Font.ColorIndex

To change the alignment of the text:

1
textBox.TextFrame.TextRange.ParagraphFormat.Alignment

To modify the margins of the textbox modify the properties:

1
2
3
4
textBox.TextFrame.MarginBottom;
textBox.TextFrame.MarginTop;
textBox.TextFrame.MarginRight;
textBox.TextFrame.MarginLeft;

Tables

Create a table using the method:

1
Word.Table table = wordDoc.Tables.Add(...);

Manipulating The Cursor

Manipulating the cursor is done with the methods and parameters in the class:

1
wordApp.Selection

Selection is just another name for ‘current cursor position’.

To jump to the last line in a document, use the method:

1
wordApp.Selection.EndKey();

To insert a break (e.g. line, page) at the current cursor position, use the method:

1
wordApp.Selection.InsertBreak(...);

Breaks (Line, Page, …)

To insert a break at the current selection point, call the following method:

1
wordApp.Selection.InsertBreak(...)

Authors

Geoffrey Hunter

Dude making stuff.

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License .

Tags

    comments powered by Disqus