简体   繁体   English

我如何从vsto插件中以编程方式加载自动化插件(dll)

[英]How do i load an automation addin (dll) programmatically from within a vsto addin

VSTO VSTO
VS2008 SP1 VS2008 SP1
.NET 3.5 .NET 3.5
Excel 2007 Excel 2007

I am a .net noob. 我是.net菜鸟。 I am trying to load an automation addin that is an excel application/automation addin (it is a dll not xla or xll) from within a vsto addin in the ThisAddIn_Startup() method of the vsto addin. 我正在尝试从vsto插件的ThisAddIn_Startup()方法中的vsto插件中加载作为Excel应用程序/自动化插件(不是xla或xll的dll)的自动化插件。 From google I got the below solution which is not working. 从谷歌我得到了下面的解决方案是行不通的。

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{

        Application excel = Globals.ThisAddIn.Application;
        //Also tried without display alerts being set to false
        excel.DisplayAlerts = false;
        foreach (AddIn addin in excel.AddIns)
        {
            if (addin.progID.Equals("MY_ADDIN_PROG_ID"))
            {
                Debug.WriteLine("Addin installed is " + addin.Installed);
                addin.Installed = false;
                Debug.WriteLine("Addin is: " + addin.FullName + ", " + addin.progID);
                Debug.WriteLine("Addin installed is " + addin.Installed);
            }
        }
        AddIn addIn = excel.AddIns.Add("MY_ADDIN_PROG_ID", false);
        addIn.Installed = true;
        excel.DisplayAlerts = true;
        Debug.WriteLine("Addin is: " + addIn.FullName + ", " + addIn.progID);
        Debug.WriteLine("Addin installed is " + addIn.Installed);
        excel.DisplayAlerts = false;
        //OTHER STARTUP CODE
        Debug.WriteLine("Starting up addin!");
}

Note, I can see the addin.installed is being set to false and back to true on startup but when I try to populate worksheet with udfs from the addin I tried to load in a later button_click method, I get #NAME? 请注意,我可以看到addin.installed被设置为false并在启动时恢复为true,但是当我尝试使用来自插件的udfs填充工作表时,我试图在以后的button_click方法中加载,得到#NAME吗? error. 错误。 I am at my wits end. 我没办法。 Any help will be greatly appreciated. 任何帮助将不胜感激。

If I first try to call the udf in excel by typing it in a cell by hand before I call my button click method, the worksheet population works and the udfs get evaluted as expected but this is not ideal. 如果在调用按钮单击方法之前先尝试在单元格中手动键入udf来调用excel中的udf,则工作表填充会起作用,并且udfs会按预期进行评估,但这并不理想。

Also setting installed property to true does not seem to be doing anything as i can still see the udf addin as inactive in excel, it is only if I type it into a cell that it gets activated. 也将已安装的属性设置为true似乎没有任何作用,因为我仍然可以在excel中将udf加载项视为非活动状态,只有当我将其键入到激活的单元格中时才可以。 Is there anything else I need to do to activate the automation addin in my vsto startup? 在我的vsto启动中,我还需要做些什么来激活自动化插件?

Thanks! 谢谢!

I'm not sure you want to do this in the startup event. 我不确定您是否要在启动事件中执行此操作。 I have done something similar but not quite the same before which may be applicable. 我做过类似但不太相同的事情,然后才适用。 I exposed some COM visible functions to VBA in a different event handler: 我在其他事件处理程序中向VBA公开了一些COM可见函数:

protected override object RequestComAddInAutomationService()
{
    // return something com-visible
}

So maybe you can try to load your automation dll this way? 因此,也许您可​​以尝试以这种方式加载自动化dll? This happens before the startup event fires... Excel might be doing something like locking its list of addins while a startup event is being handled - who knows? 这是在启动事件触发之前发生的... Excel在执行启动事件时可能正在执行诸如锁定其加载项列表的操作-谁知道? If it were possible to know Excel programming would be less tedious. 如果有可能知道Excel编程将不再那么乏味。

It is harder than it seems to combine VSTO and Automation in Excel. 这比在Excel中将VSTO和Automation结合起来似乎要困难得多。 You may find my blog post helpful: 您可能会发现我的博客文章有帮助:

Communicating Between VSTO and UDF's in Excel 在Excel中的VSTO和UDF之间进行通信

Just need to add String Value to the following registry key and you are good. 只需将String Value添加到以下注册表项中,您就可以了。

For Office 2007 对于Office 2007

Find regkey, HKEY_CURRENT_USER\\SOftware\\Microsoft\\Office\\12.0\\Excel\\Options , then create string value, where name = OPEN , value = /A "YOUR ADDIN NAME HERE" (quotes need to be included as well.) 查找regkey, HKEY_CURRENT_USER \\ SOftware \\ Microsoft \\ Office \\ 12.0 \\ Excel \\ Options ,然后创建字符串值,其中name = OPEN ,value = / A“您的ADDIN NAME HERE” (也需要包括引号。)

Note that for the first addin, value name should be called OPEN , for the second one and onwards, use OPEN1 , OPEN2 , ... etc. 请注意,对于第一个加载项,值名称应称为OPEN ,对于第二个及以后的名称,请使用OPEN1OPEN2等。

For Office 2010 对于Office 2010

Just replace 12.0 with 14.0 in the above regkey path, the rest are all the same. 只需在上述regkey路径中将12.0替换 14.0 ,其余的都一样。

Check out below article on MSDN, which will also help you a lot. 在MSDN上查看下面的文章,这也将对您有很大帮助。

http://support.microsoft.com/kb/291392 http://support.microsoft.com/kb/291392

Looks like this is a bug specific to VSTO. 看起来这是特定于VSTO的错误。 I converted my addin to a COM addin and was able to use the automation addin from code after that. 我将加载项转换为COM加载项,之后便可以使用代码中的自动化加载项。 My team has sent the issue to microsoft so we'll see what they say. 我的团队已将问题发送给Microsoft,因此我们将看看他们怎么说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM