Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. private void btnExportWord_Click_1(object sender, EventArgs e)
  2. {
  3. object oMissing = Missing.Value;
  4. Word.Application oWord = new Word.Application();
  5. Word.Document oWordDoc = new Word.Document();
  6. oWord.Visible = false;
  7. oWordDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
  8. Object oTemplatePath = @"C:testMyXMLTemplate.dotx";
  9. oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
  10.  
  11. foreach (Word.Field myMergeField in oWordDoc.Fields)
  12. {
  13. Word.Range rngFieldCode = myMergeField.Code;
  14. String fieldText = rngFieldCode.Text;
  15. if (fieldText.StartsWith(" MERGEFIELD"))
  16. {
  17. Int32 endMerge = fieldText.IndexOf("\");
  18. Int32 fieldNameLength = fieldText.Length - endMerge;
  19. String fieldName = fieldText.Substring(11, endMerge - 11);
  20. fieldName = fieldName.Trim();
  21.  
  22. for (int i = 0; i < treeViewXMLFiles.Nodes[0].Nodes.Count; i++)
  23. {
  24. string strFilename=treeViewXMLFiles.Nodes[0].Nodes[i].Text;
  25. string strFull_path=treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[0].Text;
  26. string strCreationTime=treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[1].Text;
  27. string strSize = treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[2].Text;
  28.  
  29. if (fieldName == "File_Name")
  30. {
  31. myMergeField.Select();
  32. oWord.Selection.TypeText(strFilename);
  33. }
  34. if (fieldName == "Full_Path")
  35. {
  36. myMergeField.Select();
  37. oWord.Selection.TypeText(strFull_path);
  38. }
  39. if (fieldName == "CreationTime")
  40. {
  41. myMergeField.Select();
  42. oWord.Selection.TypeText(strCreationTime);
  43. }
  44. if (fieldName == "Size")
  45. {
  46. myMergeField.Select();
  47. oWord.Selection.TypeText(strSize);
  48. }
  49. }
  50. }
  51. }
  52. Object oSaveAsFile = (Object)@"C:testFINISHED_XML_Template.doc";
  53. oWordDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
  54. ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  55. ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  56. ref oMissing, ref oMissing);
  57.  
  58. oWordDoc.Close(false, ref oMissing, ref oMissing);
  59. oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement