自动化 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;
}
}
}
效果如下: