Populate the Word template with data

Source: Internet
Author: User

Now there is a need to populate a Word document with specific data, for example, we have a terminal to print employee payroll proof, for a company, his salary certificate template is fixed, the change is the employee name, department, position and so on. All we need to do is fill in the specified data to the specified location.

1. Make a Word template,

In Word, there's a thing called bookmarks, and we can pre-insert a particular name in the place where it needs to be populated.

2. Write specific data at the specified bookmark location:

Msword.application app =Newmsword.application (); stringDestPath ="proof of salary. docx"; stringTargetPath =System.IO.Path.Combine (Directory.GetCurrentDirectory (), destpath); Msword.document Doc= App. Documents.Open (TargetPath, Visible:false); Msword.bookmark Bmname= Doc. Bookmarks.oftype<msword.bookmark> (). First (BM = BM). Name = ="Bookmark_name"); if(Bmname! =NULL) {BmName.Range.Text="Tom Wang"; } Msword.bookmark bmdept= Doc. Bookmarks.oftype<msword.bookmark> (). First (BM = BM). Name = ="bookmark_department"); if(Bmdept! =NULL) {BmDept.Range.Text="Department"; } Msword.bookmark bmjob= Doc. Bookmarks.oftype<msword.bookmark> (). First (BM = BM). Name = ="Bookmark_job"); if(Bmjob! =NULL) {BmJob.Range.Text="Software engineer"; } Msword.bookmark bmsalary= Doc. Bookmarks.oftype<msword.bookmark> (). First (BM = BM). Name = ="bookmark_salary"); if(Bmsalary! =NULL) {BmSalary.Range.Text="10000"; } Msword.bookmark bmdate= Doc. Bookmarks.oftype<msword.bookmark> (). First (BM = BM). Name = ="bookmark_date"); if(Bmdate! =NULL) {BmDate.Range.Text=DateTime.Now.ToShortDateString (); } doc.    Save (); Doc.    Close ();    Marshal.ReleaseComObject (DOC); Marshal.ReleaseComObject (app);

Run the program to insert the required data into the specified location.

Thank you for reading.

Populate the Word template with data

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.