Question

I have created a Word template project in C# using VS 2012. During run it's directly generating a Word document template with the required details. All I need is to open a Windows form first and get the user data, and using those data it need to fill the corresponding template.

I need all your ideas regarding this and sample example if you guys could.

Was it helpful?

Solution

I get the impression you are talking about a Word Mail Merge, using your c# code to provide the fields for the merge.

Using the word interops (special dlls MS provides) you can perform this along the lines of:

Application word = new Word.Application();

word.Documents.Open ( "C:\sometemplate.doc" );
word.ActiveDocument.Select ( );

Range range = word.ActiveDocument.Range ( 0, 0 );


word.ActiveDocument.Fields.Add ( range, -1, "SET field 'value'", true );


word.ActiveDocument.Fields.Update ( );
word.ActiveDocument.Fields.Unlink ( );
word.ActiveDocument.Save ( );
word.ActiveDocument.Close ( );

I suggest that you do close the document and then use Process.Start() to open it again as the Word interops can be a big buggy when it comes to memory handling.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top