C # copy and replace the content of a Word document

Source: Internet
Author: User

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,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.