【问题标题】:Creating a callable function in a VSTO office addin - COMAddIns.Object is always Nothing在 VSTO 办公室插件中创建可调用函数 - COMAddIns.Object 始终是 Nothing
【发布时间】:2019-01-25 08:20:08
【问题描述】:

我希望能够从 Excel VBA 宏调用 VSTO 插件中的函数。为了测试原理,我有以下 C# 代码。

namespace ExcelAddIn1
{
    [ComVisible(true)]
    public interface IThisAddIn
    {
        String GetText();
    }

    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    public partial class ThisAddIn : IThisAddIn
    {    
        public String GetText()
        {
            return "Now is the winter of dicontent made glorius summer by this son of York";
        }       
    }
}

我有以下 VBA 脚本

Public Sub RunTest()
    Dim txt As String
    Dim AddInList As Object
    Dim ExcelAddIn1 As COMAddIn
    Dim ThisAddIn As Object

    Set ExcelAddIn1 = Application.COMAddIns("ExcelAddIn1")

    txt = ExcelAddIn1.Object.GetText()
    Sheets(1).Cells(2, 1).Value = txt
End Sub

ExcelAddIn1.Object = 无

https://docs.microsoft.com/en-gb/visualstudio/vsto/walkthrough-calling-code-in-a-vsto-add-in-from-vba 上的 Microsoft 演练似乎有点混乱。

我想我可能需要添加RequestComAddInAutomationService,但它不像演练中描述的那样工作。当我尝试在 RequestComAddInAutomationService 中实例化我的类的实例时,我收到错误消息,基本上告诉我需要提供 ApplicationFactory 和 IServiceProvider 作为参数。

private ThisAddIn utilities;

protected override object RequestComAddInAutomationService()
{
    if (utilities == null)
       utilities = new ThisAddIn();

    return utilities;
}

【问题讨论】:

    标签: c# excel vba com vsto


    【解决方案1】:

    派生自 IThisAddIn 的类必须不同于主 vsto 类(如 microsoft 示例中所示),因此只需将您的代码替换为:

      [ComVisible(true)]
        public interface IComAddIn
        {
            String GetText();
        }
    
        [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.None)]
        public  class AddInUtilities : IComAddIn
        {
            public String GetText()
            {
                return "Now is the winter of dicontent made glorius summer by this son of York";
            }
        }
    

    在 vsto 中:

        public partial class ThisAddIn
        {
            private AddInUtilities utilities;
    
            protected override object RequestComAddInAutomationService()
            {
                if (utilities == null)
                    utilities = new AddInUtilities();
    
                return utilities;
            }
      ....
    

    此外,当您在 VBA 中调用 Application.COMAddIns 时,请确保您作为参数提供的字符串与您的 vsto 项目的 AssemblyTitle 相对应。

    PS 不需要“注册 com 互操作”。

    【讨论】:

    • 这似乎应该是正确的答案。现在一切都在编译和运行良好,但 ExcelAddIn1.Object 仍然是 Nothing。 VBA 中 COMAddIns 的字符串参数是正确的 - 否则它会落在那个点上。这里一定遗漏了一些明显的东西。
    • 也许是 VBA 中的 Set 关键字? Set MyNoNMissingObject = ExcelAddIn1.Object 然后MyNoNMissingObject.GetText()
    • 是的 - 试过了。还按照Andrew Whitechapel的建议在界面中添加了[InterfaceType(ComInterfaceType.InterfaceIsDual)]
    • 啊哈!只是注意到我的班级声明没有标记为公开。 ComInterfaceType.InterfaceIsDual 似乎没有任何区别。
    猜你喜欢
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多