【问题标题】:Call Excel Add-In function in macro在宏中调用 Excel 加载项函数
【发布时间】:2016-05-15 10:32:48
【问题描述】:

我正在为 Excel 2013 开发插件,并在 Excel 插件中创建了一个函数,如下所示

  public string ExcelReturnString()
    {
        return "This is the string: hi";
    }

我用下面的代码调用了这个函数,但是它抛出了一个错误。

Application.Run(ExcelReturnString)

如何在宏中调用插件函数?

【问题讨论】:

    标签: c# vba excel excel-addins


    【解决方案1】:

    这与直截了当的事情相去甚远,但这就是您完成任务的方式。我会尽可能明确,因为前两三次我试图这样做,我错过了很多。

    首先,当您创建承载ExcelReturnString() 的类时,您需要使用具有以下属性的接口来装饰该类,然后还标记要公开的每个方法的属性。为了这个示例,我制作了加载项类“TestExcelAddIn”:

    using System.Data;
    using System.Runtime.InteropServices;
    using Excel = Microsoft.Office.Interop.Excel;
    
    namespace TestExcelAddIn
    {
        [ComVisible(true)]
        [InterfaceType(ComInterfaceType.InterfaceIsDual)]
        public interface IStringGetter
        {
            string ExcelReturnString();
        }
    
        [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.None)]
        public class StringGetter : IStringGetter
        {
            public string ExcelReturnString()
            {
                return "This is the string: hi";
            }
        }
    }
    

    然后,在项目中与“Excel”相关联的主类中,您必须按以下方式覆盖RequestComAddInAutomationService。再一次,我包含了所有内容,所以你知道哪个类是哪个(我第一次读的时候不知道)。

    namespace TestExcelAddIn
    {
        public partial class ExcelTest
        {
            private StringGetter myAddIn;
    
            protected override object RequestComAddInAutomationService()
            {
                if (myAddIn == null)
                    myAddIn = new StringGetter();
    
                return myAddIn;
            }
    
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #region VSTO generated code
            #endregion
        }
    }
    

    现在 VBA 已准备好以下列方式使用此方法:

    Sub Test()
    
        Dim addin As Office.COMAddIn
        Dim automationObject As Object
        Dim returnString As String
    
        Set addin = Application.COMAddIns("TestExcelAddIn")
        Set automationObject = addin.Object
    
        returnString = automationObject.ExcelReturnString
    
    End Sub
    

    你本可以给我 100 年的时间来解决这个问题,而我不会。实际上归功于 MSDN 上的罗塞塔石碑:

    https://msdn.microsoft.com/en-us/library/bb608621.aspx?f=255&MSPPError=-2147217396

    【讨论】:

    • 感谢 Hambone 的回复。这对我帮助很大。
    【解决方案2】:

    除了上面 DaveMac 的注释之外,在调用另一个例程时还要记住几点:

    如果您从与该例程位于同一工作簿/插件中的例程调用宏,则不必使用 Application.Run。您可以使用它的名称来调用它:

    MyMacro
    

    如果您正在调用位于不同工作簿中的宏,那么您确实需要使用 Application.Run,​​但您还需要使用宏所在的工作簿名称,否则 VBA 将不知道它应该在哪里寻找宏:

    Application.Run "'My Fancy Spreadsheet.xlsm!'MyMacro"
    

    【讨论】:

    • 不要打败这匹死马,但方法是在 COM 加载项中,而不是在 VBA 项目中......
    【解决方案3】:

    您的代码似乎是 java。

    例如,Excel 使用 Visual basic。

    Function excelreturnstring()
        excelreturnstring = "this is the string: hi"
    End function
    

    【讨论】:

    • OP 正在谈论一个加载项,在这种情况下似乎是 C#。实际的方法是一个简单的方法,以证明他无法从 VBA 访问加载项方法——我不认为他真的想知道如何在 VBA 中编写这个特定的函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多