• Word 2003 Excel 2003 的迟绑定(late binding) 方法 时空地图TimeGIS


    在Internet Explorer地址栏里面输入word2003, excel2003, pdf等文件名是可以打开该文件的,
    (不知道office 2007支持这个功能否?)
    编程时候你也许需要动态加载或者说迟绑定打开Office 2003文件,你可以这么做

    AxSHDocVw.AxWebBrowser myBrowser = new AxSHDocVw.AxWebBrowser(); // defined in SharpDevelop
    myBrowser.Navigate(OfficeFile);

    然后打开word 2003, excel 2003 并调用Save方法可以这么做

      public override void SaveOffice()
      {
       base.SaveOffice();

       if (myApplicationType == InfoVistaConst.WordType)
       {
        try
        {
         object oApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
         if (oApp != null)
         {
          //object oDocuments = oApp.GetType().InvokeMember("Documents", BindingFlags.GetProperty, null, oApp, null);
          //oDocuments.GetType().InvokeMember("Save", BindingFlags.InvokeMethod, null, oDocuments, null);

          object oWindow = oApp.GetType().InvokeMember("ActiveWindow", BindingFlags.GetProperty, null, oApp, null);
          object oDocument = oWindow.GetType().InvokeMember("Document", BindingFlags.GetProperty, null, oWindow, null);
          oDocument.GetType().InvokeMember("Save", BindingFlags.InvokeMethod, null, oDocument, null);
         }
        }
        catch (Exception)
        {
        }
       }

       if (myApplicationType == InfoVistaConst.ExcelType)
       {
        try
        {
         object oApp;
         object oBook;
         object oBooks;
         //object oSheets;
         //object oSheet;
         //object[] Parameters;

         // Get the class type and instantiate Excel.
         //Type objClassType = Type.GetTypeFromProgID("Excel.Application");
         //object oApp = Activator.CreateInstance(objClassType);
         oApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

         if (oApp != null)
         {
          //Get the workbooks collection.
          oBooks = oApp.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, null, oApp, null);

          //object[] argsForOpen = new object[3];
          //argsForOpen[0] = OfficeFile;
          //argsForOpen[1] = true;
          //argsForOpen[2] = true;
          //oBook = oBooks.GetType().InvokeMember("Open", BindingFlags.InvokeMethod, null, oBooks, argsForOpen);

          //oBook = oBooks.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, oBooks, null);

          oBook = oApp.GetType().InvokeMember("ActiveWorkbook", BindingFlags.Default | BindingFlags.GetProperty, null, oApp, null);

          oBook.GetType().InvokeMember("Save", BindingFlags.InvokeMethod, null, oBook, null);

          ////Get the worksheets collection.
          //oSheets = oBook.GetType().InvokeMember("Worksheets", BindingFlags.GetProperty, null, oBook, null);

          ////Get the first worksheet.
          //Parameters = new Object[1];
          //Parameters[0] = 1;
          //oSheet = oSheets.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, oSheets, Parameters);

          //Return control of Excel to the user.
          //Parameters = new Object[1];
          //Parameters[0] = true;
          //oApp.GetType().InvokeMember("Visible", BindingFlags.SetProperty, null, oApp, Parameters);
          //oApp.GetType().InvokeMember("UserControl", BindingFlags.SetProperty, null, oApp, Parameters);
         }

         Application.DoEvents();
        }
        catch (Exception ex)
        {
         MessageBox.Show(ex.Message);
        }
       }
      }

    调用 Quit 方法可以这么做
      
      public void QuitOffice()
      {
       if (myApplicationType == InfoVistaConst.ExcelType)
       {
        try
        {
         object oApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
         if (oApp != null)
         {
          oApp.GetType().InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, oApp, null);
          oApp = null;
         }
        }
        catch (Exception)
        {
        }
       }

       if (myApplicationType == InfoVistaConst.WordType)
       {
        try
        {
         object oApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
         if (oApp != null)
         {
          oApp.GetType().InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, oApp, null);
          oApp = null;
         }
        }
        catch (Exception)
        {
        }
       }

      }

    有时候关闭Office相关进程可以这么做

      private void KillOffice()
        {
       if (myApplicationType == InfoVistaConst.ExcelType)
       {
        try
        {
         object obj = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
         if (obj != null)
         {
          System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
          obj = null;
         }

         foreach (Process proc in Process.GetProcessesByName("EXCEL"))
         {
          proc.Kill();
         }
         GC.Collect();
         Application.DoEvents();
        }
        catch (Exception)
        {
        }
       }
       if (myApplicationType == InfoVistaConst.WordType)
       {
        try
        {
         object obj = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
         if (obj != null)
         {
          System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
          obj = null;
         }

         foreach (Process proc in Process.GetProcessesByName("WINWORD"))
         {
          proc.Kill();
         }
         GC.Collect();
         Application.DoEvents();
        }
        catch (Exception)
        {
        }
       }
      }
      
      在 快手(http://www.kuaishou.net/) 工具里面就用了以上代码。
      这也许不是最佳办法关闭进程,希望可以指正。

        
      进一步的资料可以从这里得到
       http://support.microsoft.com/default.aspx?scid=kb;en-us;Q302902
      
      

  • 相关阅读:
    JSP 编码解说
    window系统之mongoDB安装,启动及如何设置为windows服务(总结)
    operator 安装
    JavaScript学习2
    JavaScript学习3
    win7的便签很好使
    COM技术内幕第九章笔记编程工作的简化
    VS乱码之UTF8篇
    必须而不是推荐尽量使用const引用参数
    查看不到网络连接(可以上网)
  • 原文地址:https://www.cnblogs.com/kuaishou/p/2360203.html
Copyright © 2020-2023  润新知