Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- //OBJECT OF MISSING "NULL VALUE"
- Object oMissing = System.Reflection.Missing.Value;
- Object oTemplatePath = "D:\MyTemplate.dotx";
- Application wordApp = new Application();
- Document wordDoc = new Document();
- wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
- foreach (Field myMergeField in wordDoc.Fields)
- {
- Range rngFieldCode = myMergeField.Code;
- String fieldText = rngFieldCode.Text;
- // ONLY GETTING THE MAILMERGE FIELDS
- if (fieldText.StartsWith(" MERGEFIELD"))
- {
- // THE TEXT COMES IN THE FORMAT OF
- // MERGEFIELD MyFieldName \* MERGEFORMAT
- // THIS HAS TO BE EDITED TO GET ONLY THE FIELDNAME "MyFieldName"
- Int32 endMerge = fieldText.IndexOf("\");
- Int32 fieldNameLength = fieldText.Length - endMerge;
- String fieldName = fieldText.Substring(11, endMerge - 11);
- // GIVES THE FIELDNAMES AS THE USER HAD ENTERED IN .dot FILE
- fieldName = fieldName.Trim();
- // **** FIELD REPLACEMENT IMPLEMENTATION GOES HERE ****//
- // THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE
- if (fieldName == "Name")
- {
- myMergeField.Select();
- wordApp.Selection.TypeText("Vivek");
- }
- }
- }
- wordDoc.SaveAs("myfile.doc");
- wordApp.Documents.Open("myFile.doc");
- wordApp.Application.Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement