如何从单独的 C# 项目调用 VSTO AddIn 方法? [英] How to call a VSTO AddIn method from a separate C# project?

查看:27
本文介绍了如何从单独的 C# 项目调用 VSTO AddIn 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C# Excel 插件项目MyExcelAddIn",它有一个公共方法 Foo() 来做一些复杂的事情.出于测试目的,该加载项还定义了一个连接到 Foo() 的工具栏按钮,因此我可以对此进行测试并验证单击该按钮是否调用了 Foo() 并执行了我希望它执行的操作.这很好.

I have a C# Excel Add-in project "MyExcelAddIn" that has a public method Foo() to do something complex. For testing purposes, the add-in also defines a toolbar button which is wired to Foo() so I can test this and verify that clicking the button calls Foo() and does what I want it to do. This is fine.

现在我想从 C# Windows 窗体项目中调用此方法.在 Windows 窗体项目中,我可以创建一个 Excel 实例并使其可见并验证我的 VSTO 加载项是否正在运行,因为我可以看到按钮并且它可以工作.但是我不知道如何从 Windows 窗体项目中以编程方式调用 Foo().我用谷歌搜索了一下,得到了MyExcelAddIn"COMAddIn 对象,但不知道如何调用 Foo().

Now I want to call this method from a C# Windows Forms project. In the Windows Forms project I can create an Excel instance and make it visible and verify that my VSTO add-in is running as I can see the button and it works. But I can't work out how to call Foo() programatically from the Windows Forms project. I've googled a bit and got as far as getting the "MyExcelAddIn" COMAddIn object, but can't work out how to call Foo().

看起来像这样:

// Create Excel and make it visible
Application excelApp = new Application();
excelApp.Visible = true;

// I know my VSTO add-in is running because I can see my test button
// Now get a reference to my VSTO add-in
Microsoft.Office.Core.COMAddIns comAddIns = _excelApp.COMAddIns;
object addinName = "MyExcelAddIn";
Microsoft.Office.Core.COMAddIn myAddin = comAddIns.Item(ref addinName);
// This works, but now what? How do I make a call on myAddin?
// Note that myAddin.Object is null...

所以我想知道如何从我的 Windows 窗体应用程序中调用 Foo().请注意,我对 Windows 窗体应用程序和加载项拥有完全控制权,我怀疑我必须对它们(尤其是加载项)进行更改,但我不知道如何执行此操作.

So I want to know what I can do to call Foo() from my Windows Forms application. Note that I have full control over both the Windows Forms application and the add-in and I suspect I have to make changes to both of them (particularly the add-in) but I have no idea how to do this.

请注意,这是一个 VS2008 C# 应用程序,我使用的是 Excel 2003.

Note that this is a VS2008 C# application and I'm using Excel 2003.

推荐答案

我正在使用 SendMessage Win32 API 来执行此操作.我的 C# 插件创建了一个NativeWindow",带有 WinForm 应用程序可以找到的唯一窗口标题.

I'm using the SendMessage Win32 API to do this. My C# Add-in creates a "NativeWindow" with a uniqe window title that the WinForm app can locate.

这篇关于如何从单独的 C# 项目调用 VSTO AddIn 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆