关键词搜索

源码搜索 ×
×

C# 对Word文档进行处理

发布2014-03-16浏览4641次

详情内容

  1. public partial class Form1 : Form
  2. {
  3. public Microsoft.Office.Interop.Word.Document document;
  4. public Microsoft.Office.Interop.Word.ApplicationClass wd = null;
  5. public string filename = null;
  6. private bool deactivateevents = false;
  7. public int iWordID = 0;
  8. public Form1()
  9. {
  10. InitializeComponent();
  11. }
  12. #region 加载文档
  13. /// <summary>
  14. ///加载需要处理的文档,备份,处理新生成的文档
  15. /// </summary>
  16. /// <param name="t_filename">需要处理的文件路径及名称,比如:C:\Users\jiankunking\Desktop\11.doc</param>
  17. /// <param name="NewFileName">新文件名(包含路径),比如:C:\Users\jiankunking\Desktop\22.doc</param>
  18. public void WordHandle(string t_filename, string NewFileName)
  19. {
  20. deactivateevents = true;
  21. filename = t_filename;
  22. if (wd == null) wd = new Microsoft.Office.Interop.Word.ApplicationClass();
  23. try
  24. {
  25. if (wd == null)
  26. {
  27. //自定义处理
  28. }
  29. if (wd.Documents == null)
  30. {
  31. //自定义处理
  32. }
  33. wd.CommandBars.AdaptiveMenus = false;
  34. wd.DocumentBeforeClose += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(OnClose);
  35. wd.DocumentOpen += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler(OnOpenDoc);
  36. wd.ApplicationEvents4_Event_Quit += new Microsoft.Office.Interop.Word.ApplicationEvents4_QuitEventHandler(OnQuit);
  37. }
  38. catch (Exception ex)
  39. {
  40. MessageBox.Show(ex.ToString());
  41. }
  42. if (document != null)
  43. {
  44. try
  45. {
  46. object dummy = null;
  47. wd.Documents.Close(ref dummy, ref dummy, ref dummy);
  48. }
  49. catch (Exception ex)
  50. {
  51. MessageBox.Show(ex.ToString());
  52. }
  53. }
  54. object fileName = filename;
  55. object newTemplate = false;
  56. object docType = 0;
  57. object isVisible = true;
  58. object missing = System.Reflection.Missing.Value;
  59. try
  60. {
  61. document = wd.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);
  62. if (document == null)
  63. {
  64. //自定义处理
  65. }
  66. //这里获取进程信息,是为了关闭进程用的
  67. //Process[] processes = Process.GetProcessesByName("WINWORD");
  68. //iWordID = processes[processes.Length - 1].Id;
  69. object newfileName = NewFileName;
  70. document.SaveAs(ref newfileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
  71. string sBKName = string.Empty;
  72. foreach (Microsoft.Office.Interop.Word.Bookmark bk in document.Bookmarks)
  73. {
  74. sBKName = bk.Name;
  75. //word中的书签操作
  76. }
  77. document.Save();
  78. }
  79. catch (Exception ex)
  80. {
  81. MessageBox.Show(ex.ToString());
  82. }
  83. deactivateevents = false;
  84. }
  85. #endregion
  86. /// <summary>
  87. /// catches Word's close event
  88. /// starts a Thread that send a ESC to the word window ;)
  89. /// </summary>
  90. /// <param name="doc"></param>
  91. /// <param name="test"></param>
  92. private void OnClose(Microsoft.Office.Interop.Word.Document doc, ref bool cancel)
  93. {
  94. if (!deactivateevents)
  95. {
  96. cancel = true;
  97. }
  98. }
  99. /// <summary>
  100. /// catches Word's open event
  101. /// just close
  102. /// </summary>
  103. /// <param name="doc"></param>
  104. private void OnOpenDoc(Microsoft.Office.Interop.Word.Document doc)
  105. {
  106. OnNewDoc(doc);
  107. }
  108. /// <summary>
  109. /// catches Word's newdocument event
  110. /// just close
  111. /// </summary>
  112. /// <param name="doc"></param>
  113. private void OnNewDoc(Microsoft.Office.Interop.Word._Document doc)
  114. {
  115. if (!deactivateevents)
  116. {
  117. deactivateevents = true;
  118. object dummy = null;
  119. doc.Close(ref dummy, ref dummy, ref dummy);
  120. deactivateevents = false;
  121. }
  122. }
  123. /// <summary>
  124. /// catches Word's quit event
  125. /// normally it should not fire, but just to be shure
  126. /// safely release the internal Word Instance
  127. /// </summary>
  128. private void OnQuit()
  129. {
  130. //wd=null;
  131. }
  132. }

该代码段可以用来实现,打开某个Word文档,对其中的内容进行修改替换,比如:修改书签或者替换书签之类的。
该代码段部分来自于: http://www.codeproject.com/cs/miscctrl/winwordcontrol.asp

类似文章可以参考:点击打开链接

相关技术文章

最新源码

下载排行榜

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载