Kamil Aktemur's Blog | Tips for Developers Menu

Permalink:

Creating Programmatically Word Documents on C#

Word documents are used everywhere. So, as programmers, we might have to create word documents for reports or anythings. Therefore we should learn creating programmatically word documents. We will use C# in this article. Why? C# is easier and faster than anothers(Java, C++) because It is integrated to all Microsoft components and APIs.

Step 1 : Preparing a Word Document

Firstly we have to create a Word document. (for example : newDocument.doc). After we open to document, we must add bookmark or bookmarks. Bookmarks is the most important thing for our project. For adding bookmark:

  1. Click you want to add  a programmatically text.
  2. Click on the Insert tab and click to bookmarks.
  3. Then you should name that you can remember easily, to the bookmark. If you have to work with many bookmarks, you will need to. Otherwise you might forget its’ names.

Step 2 : Create a project on Visual Studio

Open to Visual Studio and You create a new C# project (recommended WinForm Application).

Firstly you have to add Microsoft Object Library in to the project. If you don’t add, you can’t build your program. For add Microsoft Object Library, you should click add reference button where is on Project Explorer.  After that, click to COM tab and look for Microsoft Object Library. When you find it, double-click to over.  So, Library will be added your project.

Step 3 : We are begining for creating word documents.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
//We called to instance from Microsoft Object Library for our project.
 
//Now, we will define first variables for parameters.
object fileName = @"C:\Template\newDocument.doc"; 
object confirmConversions = Type.Missing; 
object readOnly = Type.Missing; 
object addToRecentFiles = Type.Missing;
object  passwordDoc = Type.Missing; 
object passwordTemplate = Type.Missing; 
object revert = Type.Missing; 
object writepwdoc = Type.Missing; 
object writepwTemplate = Type.Missing; 
object format = Type.Missing; 
object encoding = Type.Missing; 
object visible = Type.Missing; 
object openRepair = Type.Missing; 
object docDirection = Type.Missing; 
object notEncoding = Type.Missing; 
object xmlTransform = Type.Missing;
 
Microsoft.Office.Interop.Word.document doc = wordApp.Documents.Open(ref fileName, ref confirmConversions, ref readOnly, ref addToRecentFiles, ref passwordDoc, ref passwordTemplate, ref revert, ref writepwdoc,ref writepwTemplate, ref format, ref encoding, ref visible, ref openRepair,ref docDirection, ref notEncoding, ref xmlTransform);
//We created a instance with defined parameters.
 
//The following function is for replacing bookmarks that created on first step.
private void ReplaceBookmarkText (Microsoft.Office.Interop.Word.document doc, string bookmarkName, string text)
{
if ( doc.Bookmarks.Exists(bookmarkName) )
{
Object name = bookmarkName;
Microsoft.Office.Interop.Word.Range range = doc.Bookmarks.get_Item( ref name ).Range;
range.Text = text;
object newRange = range;
doc.Bookmarks.Add(bookmarkName, ref newRange);
}
}

If you call to this function, you will see changing bookmars texts. For example, you defined a bookmark that named NameSurname.  If you run function that ReplaceBookmarkText (doc, NameSurname, Joe Doe), Text of Bookmark named NameSurname will change to Joe Doe.

  • Thanks for leaving a comment, please keep it clean. HTML allowed is strong, code and a href.