自动化 Office 的客户端程序既可以创建 Office 程序的新实例,也可以获取对已在运行的实例的引用。 Microsoft 通常建议您创建一个新实例,而不是附加到正在运行的实例。 但是,在某些情况下,客户端程序必须自动执行 Office 程序的已运行实例。 在这种情况下,自动化客户端从正在运行的对象表(ROT)中获取对自动化服务器的组件对象模型(COM)对象的引用。
如果自动化服务器在运行对象表中注册自己,则 .NET 客户端可以通过调用以下命令获取对正在运行的实例的引用:

GetActiveObject 中的 InteropServices(注意添加引用using System.Runtime.InteropServices;)或 -BindToMoniker 中的 InteropServices
关键代码如下:

//添加引用
using Word = Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
//一是GetActiveObject中的InteropServices方法
Word.Application wdapp = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") as Word.Application;
//二是BindToMoniker中的InteropServices方法,可以通过使用文件名称获取对工作簿对象的引用。
Word.Application wdapp=(Word.Application)System.Runtime.InteropServices.Marshal.BindToMoniker("文件名");
//区别: GetActiveObject 获取引用由于可能会有多个 Word 实例在运行,因此`GetActiveObject` 可能会返回您不需要的实例。如果要获取对特定运行的 Word、Excel 或 Microsoft Access 实例的自动化引用,请使用 `BindToMoniker`,其中包含在该实例中打开的文件的名称。

完整代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;namespace WindowsFormsAppTest
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){}private void button1_Click(object sender, EventArgs e){//Excel Application ObjectExcel.Application oExcelApp;this.Activate();//Get reference to Excel.Application from the ROT.oExcelApp = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");//Display the name of the object.MessageBox.Show(oExcelApp.ActiveWorkbook.Name);//Release the reference.oExcelApp = null;}private void button2_Click(object sender, EventArgs e){Excel.Workbook xlwkbook;Excel.Worksheet xlsheet;//Get a reference to the Workbook object by using a file moniker.//The xls was saved earlier with this file name.xlwkbook = (Excel.Workbook)System.Runtime.InteropServices.Marshal.BindToMoniker(@"‪C:\Users\gzt\Desktop\夫人.xlsx");string sFile =textBox1.Text.Substring(textBox1.Text.LastIndexOf("\\") + 1);xlwkbook.Application.Windows[sFile].Visible = true;xlwkbook.Application.Visible = true;xlsheet = (Excel.Worksheet)xlwkbook.ActiveSheet;xlsheet.Visible = Excel.XlSheetVisibility.xlSheetVisible;xlsheet.Cells[1, 1] = 100;//Release the reference.xlwkbook = null;xlsheet = null;}private void button3_Click(object sender, EventArgs e){Word.Application wdapp;//Shell WordSystem.Diagnostics.Process.Start(@"C:\Program Files\Office2016\Office16\WINWORD.EXE");this.Activate();//Word and other Office applications register themselves in//ROT when their top-level window loses focus. Having a MessageBox//forces Word to lose focus and then register itself in the ROT.MessageBox.Show("Launched Word");//Get the reference to Word.Application from the ROT.wdapp =(Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");//Display the name.MessageBox.Show(wdapp.Name);//Release the reference.wdapp = null;}}
}

效果如下:

C#制作Office插件时引用Microsoft.Office.Interop.Word后获取运行实例相关推荐

  1. 无法引用Microsoft.Office.Interop.Excel的解决

    微软MSDN文章: http://msdn.microsoft.com/zh-cn/library/aa159923(office.11).aspx 下载 在 Microsoft Office 200 ...

  2. 引用Microsoft.Office.Interop.Excel的解决方法

    错误 14 命名空间"Microsoft.Office"中不存在类型或命名空间名称"Interop"(是缺少程序集引用吗?) 解决方法: 需要引用Microso ...

  3. 关于C#如何引用Microsoft.Office.Interop.Excel

    关于C#如何引用Microsoft.Office.Interop.Excel 1.添加相关包 2.搜索Microsoft.Office.Interop.Excel 3.查看引用 4.添加using 5 ...

  4. C#项目添加excel引用Microsoft.Office.Interop.Excel.dll

    C#项目添加excel引用Microsoft.Office.Interop.Excel.dll zgscwxd 2019-01-21 15:40:26  2286  收藏 4 展开 如果需要使用C#处 ...

  5. C#处理excel引用Microsoft.Office.Interop.Excel

    如果需要使用C#处理excel,需要引用Microsoft.Office.Interop.Excel;如何找到这个dll文件啊. (1)windows 全盘搜索 Microsoft.Office.In ...

  6. 解决引用MICROSOFT.OFFICE.CORE

    1. 解决引用Microsoft.Office.Core --> Interop.Microsoft.Office.Core.dll 直接右击引用-->添加引用-->在COM页面的下 ...

  7. wpsmac和pc版的区别_WPS Office for Mac VS Microsoft Office 365,同为办公软件,差别竟然这么大!...

    原标题:WPS Office for Mac VS Microsoft Office 365,同为办公软件,差别竟然这么大! 说起日常办公三件套:Word.PPT.Excel,目前做的常用的办公软件有 ...

  8. Microsoft Office 2003 SP3和Microsoft Office Professional Plus 2010 SP1 VOL完美共存安装教程

    Microsoft Office 2003 SP3和Microsoft Office Professional Plus 2010 SP1 VOL 完美共存安装教程 操作系统:Windows 7 Ul ...

  9. 使用Office组件读取Excel,引用Microsoft.Office.Interop.Excel出现的问题

    操作背景:asp.net操作Excel 出现问题:在本地添加引用(com):Microsoft Office 11.0 Object Library,并写好程序调试正常,部署到服务器时,出现异常 Ex ...

最新文章

  1. 二十五:设计模式的总结
  2. div 图片滚动 / 文字滚动
  3. python数据分析常用类库(1.1)
  4. SpringCloud Gateway 服务网关,过滤器
  5. python没有错误但是不显示结果_python运行不报错又无任何结果输出
  6. react antd 更改table 表头和表行样式
  7. 《流畅的Python》读书笔记——Python使用一等函数实现设计模式
  8. SpringMVC入门(一)Dispatcher
  9. B站上这些Python和数据分析视频真香!
  10. 详细过程!SpreadJS助力企业轻松构建跨域提交、数据分析、协同编辑一体化云表单
  11. 普林斯顿微积分读本02第一章--函数的复合、奇偶函数、函数图像
  12. 微信小程序获取二维码:报错47001 data format error
  13. sql语句的插入方式总结
  14. 融入动画技术的交互应用优秀作业推荐
  15. egg+vue+mongodb实践开发在线文档管理平台——水墨文档
  16. 云端服务器跑python代码,断开后台运行
  17. Source not fount
  18. 基本DOS命令---3.修改文件夹和文件属性
  19. vlookup匹配值不唯一_十大Vlookup常见错误!
  20. 芯片封装的作用以及常见封装技术

热门文章

  1. 服务器tcp连接上限问题解决:报错代码10060 由于连接方在一段时间后没有正确答复或连接的主机没有反应
  2. canvas是什么牌子的包_CELINE的包包怎么样?
  3. 用微信小程序开店之六——小程序组件2:“基础内容”
  4. Win10 64位 Delphi XE10 TClientDataset 访问 DataSnap 服务端故障解决
  5. 不用 996,弹性时间搭配各种福利,来美餐做我的同事吗?
  6. 翻译:Vulkan VK_EXT_extended_dynamic_state 介绍
  7. 自动写代码机(其实是自动抄代码机,含部分KeyEvent值对应表)
  8. Delphi WebBrowser 去除滚动条和边框
  9. 最新模型-SUMBT【slot-utterance matching belief tracker】
  10. #学习笔记#(58)中文标点符号转英文标点符号,及全角转半角js