简体   繁体   中英

How to insert/fetch a cover page in word document using Microsoft.Office.Interop.Word C#

I'm creating a MsOffice template application(Winforms) to insert/evaluate the word document.

I want to insert a cover page and later after changes in cover page then i want to evaluate it, using interop c#. I searched a lot on internet but i didn't find suitable one.

Can any one please help me.

Thanks

So if your word template is the same (If the document already exists) each time you essentially have to:

  • Copy The Template
  • Work On The Template
  • Save In Desired Format
  • Delete Template Copy

Each of the sections that you are replacing within your word document you have to insert a bookmark for that location (easiest way to input text in an area).

I always create a function to accomplish this, and I end up passing in the path - as well as all of the text to replace my in-document bookmarks. The function call can get long sometimes, but it works for me.

Application app = new Application();
Document doc = app.Documents.Open("sDocumentCopyPath.docx");

if (doc.Bookmarks.Exists("bookmark_1"))
{
    object oBookMark = "bookmark_1";
    doc.Bookmarks.get_Item(ref oBookMark).Range.Text =
            "My Text To Replace bookmark_1";
}
if (doc.Bookmarks.Exists("bookmark_2"))
{
    object oBookMark = "bookmark_2";
    doc.Bookmarks.get_Item(ref oBookMark).Range.Text =
            "My Text To Replace bookmark_2";
}

doc.ExportAsFixedFormat("myNewPdf.pdf", WdExportFormat.wdExportFormatPDF);

((_Document)doc).Close();
((_Application)app).Quit();

The above code will get inserting text working for you - is there a reason that you have to re-evaluate the document afterwards if you know (and can add in checks before you attempt to insert ie: if the bookmark doesn't exist).

If you need some more explanation I can help as well :) my example saves it as a .pdf, but you can do any format you prefer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM