www.TimeGIS.com

时空地图 www.TimeGIS.com

交互式图形化建模仿真软件开发

导航

Word 2003 Excel 2003 的迟绑定(late binding) 方法

在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
  
  

 

 

posted on 2008-12-02 22:14  时空地图-TimeGIS-com  阅读(795)  评论(0编辑  收藏  举报

www.TimeGIS.com