The requirement of a recent project is to replace the content of the character in the record set with the specific content of the Word document template to generate different documents. In two steps:
1. Copy the template content to a Document object. Copy the content from the source DOC Document and return a Document class. # region returns a Document class from the source DOC Document copy content.
/// <Summary>
/// Copy the content from the source DOC file and return a Document class
/// </Summary>
/// <Param name = "sorceDocPath"> source DOC file path </param>
/// <Returns> Document </returns>
Protected Document copyWordDoc (object sorceDocPath)
{
Object objDocType = WdDocumentType. wdTypeDocument;
Object type = WdBreakType. wdSectionBreakContinuous;
// Word application variable
Application wordApp;
// Word document variable
Document newWordDoc;
Object readOnly = false;
Object isVisible = false;
// Initialization
// Because the COM library is used, many variables need to be replaced by Missing. Value.
WordApp = new ApplicationClass ();
Object Nothing = System. Reflection. Missing. Value;
// WordDoc = wordApp. Documents. Add (ref Nothing, ref Nothing );
NewWordDoc = wordApp. Documents. Add (ref Nothing, ref Nothing );
Document openWord;
OpenWord = wordApp. Documents. Open (ref sorceDocPath, ref Nothing, ref readOnly, ref Nothing, ref Nothing,