从C#调用Outlook中的VBA代码 [英] Call Outlook VBA code from c#
问题描述
在Visual Studio中,我创建一个插件,在addin_startup我设置的Outlook应用程序
应用程序=(微软.Office.Interop.Outlook.ApplicationClass)(Marshal.GetActiveObject(Outlook.Application));
然后我打电话一runMacro功能,这是我从MSDN获得
私人无效RunMacro(对象oApp,对象[] oRunArgs)
{
试
{
oApp.GetType()InvokeMember(运行,System.Reflection.BindingFlags.InvokeMethod,空,oApp,oRunArgs)。
}
赶上(例外五)
{
MessageBox.Show(e.GetType()的ToString());
}
}
我通过这个功能,我的Outlook应用对象和宏的名称数组中的运行,像这样...
RunMacro(应用程序,新的对象[ ] {showFormDisplay});
我得到下面的异常
未知的名称。 (从HRESULT异常:0x80020006(DISP_E_UNKNOWNNAME))
我不知道为什么你有你确切的问题,我会承担一个或多个参数不正确,但不知道,因为我从来没有做过它,你正在试图做到这一点。
我建议使用有所不同的代码,你也许可以在你的代码重用看着下面的文章为一个完整的示例:的如何:从Visual C#.NET
使用自动化运行Office宏
In visual studio I am creating an addin, in the addin_startup I am setting an Outlook application to
app = (Microsoft.Office.Interop.Outlook.ApplicationClass)(Marshal.GetActiveObject("Outlook.Application"));
then I am calling a runMacro function which I got from msdn
private void RunMacro(object oApp, object[] oRunArgs)
{
try
{
oApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs);
}
catch (Exception e)
{
MessageBox.Show(e.GetType().ToString());
}
}
I pass this function, my Outlook app object and the name of the macro to run in an array, as so...
RunMacro(app, new Object[] { "showFormDisplay" });
I get the following exception
Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
I don't know why you have your exact issue, I'd assume one or more parameters is incorrect but not sure since I've never done it the way you're trying to do it.
I'd suggest looking at the following article for a complete sample using somewhat different code, that you might be able to reuse in your code: HOW TO: Run Office Macros by Using Automation from Visual C# .NET
这篇关于从C#调用Outlook中的VBA代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!