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