...
Include the SoftArtisans.OfficeWriter.WordWriter namespace in the code behind.
Code Block language c# using SoftArtisans.OfficeWriter.WordWriter;
Code Block language vb Imports SoftArtisans.OfficeWriter.WordWriter
Instantiate the WordApplication object
Code Block language c# WordApplication WAPP = new WordApplication();
Code Block language vb Dim WAPP As WordApplication = new WordApplication()
Create a new document with WordApplication.Create()
Code Block language c# Document DOC = WAPP.Create();
Code Block language vb Dim DOC As Document = WAPP.Create()
Unlike the WordTemplate object, which represents a single file, WordApplication works as a file generation engine. The WordApplication object can be used to create, open, and save multiple documents at once.
Note WordApplication only supports the Office 2003 file formats - DOC, DOT. WordWriter does not currently support creating DOCX/DOCM files with WordApplication.
For this example, we'll pull a value from a text box in a web form. Get the value.
Code Block language c# string value = DataValueBox.Text.Trim();
Code Block language vb Dim value As string = DataValueBox.Text.Trim()
Insert the text into the document with Element.InsertTextAfter.
Code Block language c# DOC.InsertTextAfter(value, true);
Code Block language vb DOC.InsertTextAfter(value, true)
Text strings need to be inserted into a paragraph, table cell, or list entry elements in a document. In this case, when the text is inserted into the document, it's actually inserted into the last paragraph in the document. Documents created by WordApplication come with a paragraph, so we do not need to create that paragraph.
Save the output file with WordApplication.Save
Code Block language c# WAPP.Save(DOC, Response, "Output.doc", false);
Code Block language vb WAPP.Save(DOC, Response, "Output.doc", false)
WordApplication.Save has the same output options as WordTemplate: save to disk, save to memory stream, stream back to the client inline, and stream back to the client as an attachment. In this case, we're streaming the document back to the client as an attachment.
Here is an example of what the output should look like:
...