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

自动化 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 Object
            Excel.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 Word
            System.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;

        }
    }
}

效果如下:
效果图

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
microsoft.office.interop.word.dll .dll 本文件能够完美解决系统中缺失microsoft.office.interop.word.dll文件等问题, microsoft.office.interop.word.dll文件官方下载。使用前请详细阅读microsoft.office.interop.word.dll丢失修复方法。 microsoft.office.interop.word.dll使用方法: 方法一 一、如果在运行某软件或编译程序提示缺少、找不到microsoft.office.interop.word.dll等类似提示 下载来的microsoft.office.interop.word.dll拷贝到指定目录即可(一般是system系统目录或放到软件同级目录里面),或者重新添加文件引用。 二、将软件包下载下来后,先将其解压(一般都是rar压缩包), 然后根据您系统的情况选择X86/X64,X86为32位电脑,X64为64位电脑。 三、直接拷贝系统对应的microsoft.office.interop.word.dll到系统目录里: 1、Windows 95/98/Me系统,将microsoft.office.interop.word.dll复制到C:\Windows\System目录下。 2、Windows NT/2000系统,将microsoft.office.interop.word.dll复制到C:\WINNT\System32目录下。 3、Windows XP/WIN7/win10系统(64位系统对应64位dll文件,32位系统对应32位dll文件),将microsoft.office.interop.word.dll复制到C:\Windows\System32目录下。 4、如果您的系统是64位的请将32位的dll文件复制到C:\Windows\SysWOW64目录。 四、打开"开始-运行-输入regsvr32 microsoft.office.interop.word.dll",回车即可解决。您提供的microsoft.office.interop.word.dll对您有所帮助。 方法二 下载中心下载dll的朋友,可将下面的代码保存为“注册.bat“,放到dll文件同级目录(只要在同一个文件夹里面有这两个文件即可),双击注册.bat,就会自动完成microsoft.office.interop.word.dll注册(win98不支持)。 下面是系统与dll版本对应的注册bat文件(64位的系统对应64位dll文件,32位系统对应32位的dll文件,如果64位的系统安装32位的dll文件,请将下面的system32替换为SysWOW64即可。) 复制代码代码如下: @echo 开始注册 copy microsoft.office.interop.word.dll%windir%\system32\ regsvr32 %windir%\system32\microsoft.office.interop.word.dll /s @echo microsoft.office.interop.word.dll注册成功 @pause

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值