Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use C# to fill out a Word document?

I have a Word document, letter.docx, that is a letter I intend to mail to hundreds of people for a party. The letter is already composed and has been formatted in its own special way with varying type sizes and fonts. It's set and ready to go, with placeholders where I have to fill out variables that change like Name, Address, phone number, etc.

Now, I would like to write a C# program where a user can type in variable things like Name, Address, etc., into a form, hit a button, and produce letter.docx with the right information filled in at the right places.

I understand Word has features that allow you do this, but I really want to do this in C#.

like image 629
phan Avatar asked Mar 07 '14 18:03

phan


People also ask

How do you use C programming?

We can use either a plain text editor (like Notepad) or the IDE's built-in editor. The source code must follow the syntax of the C programming language. After the source file is complete, save it as a *.c file. We'll need a compiler to compile our source code.

Why is C good for beginners?

The programs that you write in C compile and execute much faster than those written in other languages. This is because it does not have garbage collection and other such additional processing overheads. Hence, the language is faster as compared to most other programming languages.

What is C and how it works?

The C programming language works by writing your code into an executable file. The C compiler will take that executable and convert it in its entirety into machine code that will then be executed by your computer at runtime.


3 Answers

Of course you can do it. Use Microsoft.Office.Interop.Word reference in your project.

First bookmark all the fields you want to be updated in the document from 'insert' tab (eg. NameField is bookmarked with tag 'name_field'). Then, in your C# code add the following:

Microsoft.Office.Interop.Word.Application wordApp = null;
wordApp = new Microsoft.Office.Interop.Word.Application();
wordApp.Visible = true;

Document wordDoc = wordApp.Documents.Open(@"C:\test.docx");
Bookmark bkm = wordDoc.Bookmarks["name_field"];
Microsoft.Office.Interop.Word.Range rng = bkm.Range;
rng.Text = "Adams Laura"; //Get value from any where

Remember to properly save & close the document.(You can see this)

like image 150
pxm Avatar answered Oct 03 '22 23:10

pxm


I don't know of anything built into the language, but the example here seems to do exactly what you want.

If you can provide specific examples of what you want to do (are the placeholders Fields? specifically name bits of text?), I can probably give you a more refined answer that directly targets your problem.

like image 38
Sean Duggan Avatar answered Oct 03 '22 22:10

Sean Duggan


Word Provides COM objects that one can make use of in C#

Add a reference to the Microsoft office interop under the COM tab in the add reference dialog

Also, see this question:

Filling in FIelds in work using C#

like image 31
Michael Kniffen Avatar answered Oct 03 '22 22:10

Michael Kniffen