利用COM组件实现对WORD书签处写入值
2018-06-22 07:18:28来源:未知 阅读 ()
using System; using System.Collections.Generic; using System.Text; using Microsoft.Office.Interop.Word; using System.Windows.Forms; using System.IO; using System.Reflection; namespace HustCAD.IntePLM.Win.BatchEnterWinUI { public class SignWord { //Word应用程序变量 _Application wordApp; _Document wordDoc; public bool signWord(string filePath, string bookMark, string code) { bool success = false; try { //由于使用的是COM库,因此有许多变量需要用Missing.Value代替 wordApp = new ApplicationClass(); object missing = System.Reflection.Missing.Value; object templateName = filePath; wordDoc = wordApp.Documents.Open(ref templateName, 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); if (wordApp.ActiveDocument.Bookmarks.Exists(bookMark)) { object bk = (object)bookMark; wordApp.ActiveDocument.Bookmarks.get_Item(ref bk).Select(); wordApp.Selection.TypeText(code); //关闭wordDoc,wordApp对象 object SaveChanges = WdSaveOptions.wdSaveChanges; object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat; object RouteDocument = false; wordDoc.Save(); wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordDoc = null; wordApp = null; success = true; return true; } else { System.IO.File.AppendAllText(System.IO.Path.Combine(GetCurrentPath(), "signWordlog.txt"), "文件" + filePath + "签入编号失败,书签不存在! " + DateTime.Now.ToString("yyyy-MM-dd-hh:mm:ss")); //关闭wordDoc,wordApp对象 object SaveChanges = WdSaveOptions.wdSaveChanges; object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat; object RouteDocument = false; wordDoc.Save(); wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordDoc = null; wordApp = null; return false; } } catch (Exception e) { object SaveChanges = WdSaveOptions.wdSaveChanges; object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat; object RouteDocument = false; //wordDoc.Save(); wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument); System.IO.File.AppendAllText(System.IO.Path.Combine(GetCurrentPath(), "signWordlog.txt"), "文件" + filePath + "签入编号失败! ,错误:" + e.Message.ToString() + "。 " + DateTime.Now.ToString("yyyy-MM-dd-hh:mm:ss")); wordDoc = null; wordApp = null; return false; } finally { try { object SaveChanges = WdSaveOptions.wdSaveChanges; object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat; object RouteDocument = false; if (wordDoc!=null&&success==false ) { wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordDoc = null; } if (wordApp != null && success == false) { wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordApp = null; } GC.Collect(); GC.WaitForPendingFinalizers(); } catch { } } } /// <summary> /// 得到当前程序的路径 /// </summary> /// <returns></returns> static public string GetCurrentPath() { string asstring = Assembly.GetExecutingAssembly().Location; string[] aa = asstring.Split('\\'); string path = string.Empty; foreach (string var in aa) { if (var != aa[aa.Length - 1]) path += var + @"\"; } return path; } } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 利用.net代码实现发送邮件 2020-02-17
- C#利用服务器实现客户端之间通信 2020-01-20
- TrieTree服务-组件构成及其作用介绍 2019-10-08
- asp.net中Fine Uploader文件上传组件使用介绍 2019-10-08
- U9创建BE组件 2019-07-24
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash