VSTO加载项,COMAddIns和RequestComAddInAutomationService [英] VSTO Add-ins, COMAddIns and RequestComAddInAutomationService

查看:854
本文介绍了VSTO加载项,COMAddIns和RequestComAddInAutomationService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅第一次修改(在底部截图):

我已经按照这篇文章有一个winform应用程序触发一个VSTO加载方法:
http://blogs.msdn.com/b/andreww/archive/2007/01/15/vsto-add-ins-comaddins-and-requestcomaddinautomationservice.aspx

I've followed this article to have a Winform app trigger a VSTO Add-In method: http://blogs.msdn.com/b/andreww/archive/2007/01/15/vsto-add-ins-comaddins-and-requestcomaddinautomationservice.aspx

在上述文章的最后笔者提到了一个问题,试图在这里改善它:
http://blogs.msdn.com/b/andreww/archive /2008/08/11/why-your-comaddin-object-should-derive-from-standardolemarshalobject.aspx

At the end of the above article the author mentions a issue and tries to ameliorate it here: http://blogs.msdn.com/b/andreww/archive/2008/08/11/why-your-comaddin-object-should-derive-from-standardolemarshalobject.aspx

我已经通过几个代码现在时代和导出StandardOleMarshalObject改善异常不起作用

I have been through the code several times now and the method to derive StandardOleMarshalObject to ameliorate the exception does not work!

System.InvalidCastException方法:无法投型系统.__ ComObject的COM对象的接口类型...此操作失败的原因是对IID

下面是一个摄制 - 这两个项目面向.NET 3.5:

Here is a repro - both projects target .Net 3.5:

a)创建一个新的Office> 2007或2010> Excel插件:

a) Create a new Office > 2007 or 2010 > Excel Add-In:

namespace ExcelAddIn1
{
    public partial class ThisAddIn
    {
        private AddinUtilities addinUtilities;
        protected override object RequestComAddInAutomationService()
        {
            if (addinUtilities == null)
            {
                addinUtilities = new AddinUtilities();
            }
            return addinUtilities;
        }
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
        }
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }
}
}

b)添加一流的Excel插件:

b) Add a class to the Excel Add-In:

namespace ExcelAddIn1
{
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IAddinUtilities
{
    void DisplayMessage();
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class AddinUtilities :
    StandardOleMarshalObject,
    IAddinUtilities
{
    void IAddinUtilities.DisplayMessage()
    {
        MessageBox.Show("Hello World");
    }
}
}



c)设置项目属性>建立> seklect注册为COM互操作。

c) Set Project Properties > Build > seklect Register For COM Interop. Compile the Add-In.

d)新的winform应用程序,引用ExcelAddIn1,和的Microsoft.Office.Interop.Excel办公室和包括在Form1中的代码>

d) New Winform App, reference the ExcelAddIn1, Microsoft.Office.Interop.Excel and Office and include this code in Form1:

public partial class Form1 : Form
{
private Microsoft.Office.Interop.Excel.Application excel;
private IAddinUtilities utils;
public Form1()
{
    InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
    excel = new Microsoft.Office.Interop.Excel.Application();
    excel.Visible = true;
    excel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
    object addinName = "ExcelAddin1";
    COMAddIn addin = excel.COMAddIns.Item(ref addinName);
    utils = (IAddinUtilities)addin.Object;
    utils.DisplayMessage();
}
}



E)运行WinForm的应用程序和行 utils的=(IAddinUtilities)addin.Object; 无论失败与否AddinUtilities从StandardOleMarshalObject派生。

e) Run the Winform app and the line utils = (IAddinUtilities)addin.Object; fails regardless of whether or not AddinUtilities derives from StandardOleMarshalObject.

我在这里损失的MSDN博客明确表示:要解决这一切,您可以简单地从StandardOleMarshalObject派生AddinUtilities类,并重建:

I am at a loss here as the MSDN Blog specifically says: "To fix all this, you can simply derive the AddinUtilities class from StandardOleMarshalObject, and rebuild:"

1日编辑:我试图在另一台PC-b的代码和它的工作没有从 StandardOleMarshalObject

1st I tried the code on another PC-B and it works without deriving from StandardOleMarshalObject,

当我与 StandardOleMarshalObject 在PC-b试过我得到了同样的问题,因为PC-A。 PC-A不与任何方式工作,我能想到的唯一的区别是管理权限。

When I tried with StandardOleMarshalObject on PC-B I got the same problem as PC-A. PC-A doesn't work with either ways and the only difference I can think of is admin rights.

管理员权限和预Win7的(旧MSDN文章)是我能想到的,为什么它不会工作的唯一原因。

Admin rights and pre-Win7 (old msdn articles) are the only reasons I can think of why it would not work.

推荐答案

在设定注册为COM Interop 我发现,你需要以管理员身份运行。

没有管理员试图编译我得到了像错误:

Without Administrator trying to compile I got errors like:

无法注册程序集 C:\TFS\Project\Src\ProjectAddin\bin\Debug
\ProjectAddin.dll - 访问被拒绝。请确保您正在运行
的应用程序作为管理员。访问注册表项
'HKEY_CLASSES_ROOT\CLSID {3A6192EA-3C9C-39EB-99A3-3DBFF8CA118F}'是
拒绝。

Cannot register assembly "C:\TFS\Project\Src\ProjectAddin\bin\Debug \ProjectAddin.dll" - access denied. Please make sure you're running the application as administrator. Access to the registry key 'HKEY_CLASSES_ROOT\CLSID{3A6192EA-3C9C-39EB-99A3-3DBFF8CA118F}' is denied.

上面的注册表项是不存在的,所以我创造了它,然后试图编译我得到了:

The above registry key didn't exist so I created it, then trying to compile I got:

无法注册类型库
C:\TFS\Project\Src\ProjectAddin\bin\Debug \ProjectAddin.tlb。错误
访问OLE注册表。 (异常来自HRESULT:0x8002801C
(TYPE_E_REGISTRYACCESS))

Cannot register type library "C:\TFS\Project\Src\ProjectAddin\bin\Debug \ProjectAddin.tlb". Error accessing the OLE registry. (Exception from HRESULT: 0x8002801C (TYPE_E_REGISTRYACCESS))

关闭注册COM互操作和错误消失 - 但是这使得它无法工作。

Solution

Turn off Register for COM interop and the error goes away - but this causes it not to work!

Shift +右键单击Visual Studio和以管理员身份打开,开该项目。勾选注册为COM Interop,它编译成功,现在从StandardOleMarshalObject派生时,它适用于PC-A - ,但它不无StandardOleMarshalObject工作 - 这是我对PC-B获得了相反的结果

Shift + Right click Visual Studio and open as Administrator, open the project. Tick Register for Com interop and it compiles successfully and it works now on PC-A when deriving from StandardOleMarshalObject - but it doesn't work without StandardOleMarshalObject - which is the opposite results I got on PC-B.

这篇关于VSTO加载项,COMAddIns和RequestComAddInAutomationService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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