C # Assigns a value to a bookmark in a Word document

Source: Internet
Author: User

1: Bookmark content that needs to be assigned in a given Word template

2: Reference Microsoft.Office.Interop.Word in the Manage NuGet package

3:object omissing = System.Reflection.Missing.Value; Create a Word application instance

Microsoft.office.interop.word._application oword = new Microsoft.Office.Interop.Word.Application (); Set to not visible

oWord.Visible = false; Template file Address

string file = "trial submissions. doc";

Object otemplate = Path.Combine (context. Server.MapPath ("/audit/"), file); ;//template path//Generate document based on template microsoft.office.interop.word._document odoc = OWORD.DOCUMENTS.ADD (ref otemp Late, ref omissing, ref omissing, ref omissing);

try {

object[] oBookMark = new Object[6]; Declaring an array of bookmarks

Assignment Book Signature

Obookmark[0] = "ProjectName"; Project name

OBOOKMARK[1] = "Submitdate"; Time of approval

OBOOKMARK[2] = "Closedtime"; Date of the statement of trial

OBOOKMARK[3] = "Personneldate"; Hearing Officer (automatically added name and date)

OBOOKMARK[4] = "Rpersonneldate"; Principal of the hearing body (name and date)

OBOOKMARK[5] = "Apersonneldate"; General Auditor's opinion (name and date)

Assigning arbitrary data to the location of a bookmark

ODoc.Bookmarks.get_Item (ref obookmark[0]). Range.Text = "project name";

ODoc.Bookmarks.get_Item (ref obookmark[1]). Range.Text = "2016-10-15";

ODoc.Bookmarks.get_Item (ref obookmark[2]). Range.Text = "2016-10-15";

ODoc.Bookmarks.get_Item (ref obookmark[3]). Range.Text = "2016-10-15";

ODoc.Bookmarks.get_Item (ref obookmark[4]). Range.Text = "2016-10-15";

ODoc.Bookmarks.get_Item (ref obookmark[5]). Range.Text = "Consent (input)";

Build word and save it in a folder

string filen = info. ProjectName + "-Trial submissions. doc";

Object filename = context. Server.MapPath ("/files/") + filen;

String path = "/files/" + filen;

Odoc.saveas (ref filename, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing , ref omissing,ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing);

oDoc.Close (ref omissing, ref omissing, ref omissing); Close Word

Oword.quit (ref omissing, ref omissing, ref omissing);

odoc = null;

oword = null;

C # Assigns a value to a bookmark in a Word document

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.