Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. static void Main(string[] args)
  2. {
  3. //OBJECT OF MISSING "NULL VALUE"
  4.  
  5. Object oMissing = System.Reflection.Missing.Value;
  6.  
  7. Object oTemplatePath = "D:\MyTemplate.dotx";
  8.  
  9.  
  10. Application wordApp = new Application();
  11. Document wordDoc = new Document();
  12.  
  13. wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
  14.  
  15. foreach (Field myMergeField in wordDoc.Fields)
  16. {
  17.  
  18.  
  19. Range rngFieldCode = myMergeField.Code;
  20.  
  21. String fieldText = rngFieldCode.Text;
  22.  
  23.  
  24.  
  25. // ONLY GETTING THE MAILMERGE FIELDS
  26.  
  27. if (fieldText.StartsWith(" MERGEFIELD"))
  28. {
  29.  
  30. // THE TEXT COMES IN THE FORMAT OF
  31.  
  32. // MERGEFIELD MyFieldName \* MERGEFORMAT
  33.  
  34. // THIS HAS TO BE EDITED TO GET ONLY THE FIELDNAME "MyFieldName"
  35.  
  36. Int32 endMerge = fieldText.IndexOf("\");
  37.  
  38. Int32 fieldNameLength = fieldText.Length - endMerge;
  39.  
  40. String fieldName = fieldText.Substring(11, endMerge - 11);
  41.  
  42. // GIVES THE FIELDNAMES AS THE USER HAD ENTERED IN .dot FILE
  43.  
  44. fieldName = fieldName.Trim();
  45.  
  46. // **** FIELD REPLACEMENT IMPLEMENTATION GOES HERE ****//
  47.  
  48. // THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE
  49.  
  50. if (fieldName == "Name")
  51. {
  52.  
  53. myMergeField.Select();
  54.  
  55. wordApp.Selection.TypeText("Vivek");
  56.  
  57. }
  58.  
  59. }
  60.  
  61. }
  62. wordDoc.SaveAs("myfile.doc");
  63. wordApp.Documents.Open("myFile.doc");
  64. wordApp.Application.Quit();
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement