I have a WPF window( using c# as code behind) that has some text fields.
What I want is, when the user presses the print button I want to take the information on these fields and use Microsoft word 2003 template. The template has some blank fields to be filled with these info coming from WPF widow.
How would I automate word to do this?
I have a WPF window( using c# as code behind) that has some text
Share
This is easy:
Add a COM reference to the the “Microsoft Word 11.0 Object Library” (or use the Microsoft.Office.Interop.Word assembly). You may have to in install Visual Studio Tools for Office System and/or browse to your the Primary Interop Assembly, depending on your VS.NET and Office versions and what else you have installed.
Create a Word.Application application object
var app = new Word.Application()Open the document with
var doc = app.Documents.Open(...). Note that in C# 3.5 or below you must pass all parameters. You can use a variable initialized to System.Reflection.Missing.Value for most of them.Iterate through doc.Fields using foreach: Read and parse the field’s
.Coderange, then update the field’s.Resultrange based on the text box content.For example:
This assumes your data context class has a DependencyProperty “LastName” that is bound from the XAML like this: