zoukankan      html  css  js  c++  java
  • 利用COM组件实现对WORD书签处写入值

    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;
            }
        }
    }
    

      

  • 相关阅读:
    outlook的签名导致新邮件使用签名时产生ActiveX警告。
    Insecure.Org 2006年度的安全工具调查
    理解windows网络/netbios/SMB/CIFS
    hp 笔记本 sata native mode安装xp
    repadmin查看活动目录内的对象属性
    Schema Object Identifiers OIDs
    普通用户设置显示DPI没有权限
    在两个页面间翻转设置Animation动作的一些总结
    Xcode3.2.6异常调试,快速定位出错行
    (转)用NSDateFormatter调整时间格式的代码
  • 原文地址:https://www.cnblogs.com/Acamy/p/6613683.html
Copyright © 2011-2022 走看看