惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Google DeepMind News
Google DeepMind News
V
V2EX
B
Blog RSS Feed
有赞技术团队
有赞技术团队
博客园 - Franky
美团技术团队
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
云风的 BLOG
云风的 BLOG
L
LangChain Blog
GbyAI
GbyAI
The Cloudflare Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
博客园 - 【当耐特】
The Register - Security
The Register - Security
大猫的无限游戏
大猫的无限游戏
D
Docker
Vercel News
Vercel News
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
P
Proofpoint News Feed
N
Netflix TechBlog - Medium
博客园_首页
A
About on SuperTechFans
J
Java Code Geeks
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
Recent Announcements
Recent Announcements
G
Google Developers Blog
小众软件
小众软件
博客园 - 叶小钗
WordPress大学
WordPress大学
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
F
Full Disclosure
Jina AI
Jina AI
H
Help Net Security

博客园 - ChinaRay.XiiX

http://wiki.kuaishou.net 你一旦开始,就无法停止 快手之友情链接 快手 2.1 发布 十大百科 快手短信免费版 [zt]LOST时空穿梭理论大揭秘 —— 迄今为止最完美最合理的对LOST的解释 快手博客 快手文档 - www.kuaishou.net 快手网址 - KuaiShou.NET 快手影音 www.kuaishou.net Delphi 使用 Visual Studio Shell , 开源为期不远 快手 (KuaiShou.NET) 之账本应用 快手之Word篇 www.kuaishou.net 快手之Excel篇 www.kuaishou.net 快手 www.kuaishou.net VeryCD WebSpider - A plugin for KuaiShou.NET InfoVista.NET Beta InfoVista 框架之前人栽树后人乘凉篇
Word 2003 Excel 2003 的迟绑定(late binding) 方法
ChinaRay.XiiX · 2008-12-02 · via 博客园 - ChinaRay.XiiX

在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