简体   繁体   English

在宏中调用Excel加载项功能

[英]Call Excel Add-In function in macro

I am developing Add-in for Excel 2013 and I have created a function in Excel Add-In as below 我正在开发Excel 2013的加载项,我在Excel加载项中创建了一个函数,如下所示

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

I have used below code to call the function, but it throws an error. 我使用下面的代码来调用函数,但它会抛出一个错误。

Application.Run(ExcelReturnString)

How can I call the Add-in function in macro? 如何在宏中调用外接功能?

This is about the farthest thing from straight-forward, but this is how you accomplish the task. 这是关于直接前进的最远的事情,但这就是你完成任务的方式。 I'm going to be as explicit as possible, because the first two or three times I tried to do this, I missed a LOT. 我会尽可能地明确,因为前两次或三次我试图这样做,我错过了很多。

First, when you create the class that hosts ExcelReturnString() , you need to decorate the class with an interface that has the following attributes and then also tag the attributes for each method you want to expose. 首先,当您创建承载ExcelReturnString()的类时,您需要使用具有以下属性的接口来装饰该类,然后还要标记要公开的每个方法的属性。 I made the add-in class "TestExcelAddIn" for the sake of this example: 为了这个例子,我创建了加载类“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";
        }
    }
}

Then, in the main class, associated with "Excel" in your project, you have to override RequestComAddInAutomationService in the following manner. 然后,在主类中,与项目中的“Excel”关联,您必须以下列方式覆盖RequestComAddInAutomationService Again, I am including EVERYTHING so you know which class is which (I didn't when I first read it). 再一次,我包括了所有内容,所以你知道哪个类是哪个(我第一次阅读时没有)。

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
    }
}

Now VBA is ready to consume this method in the following manner: 现在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

You could have given me 100 years to figure this out, and I would not have. 你可以给我100年的时间来解决这个问题,但我不会。 Actually credit MSDN for the Rosetta stone on it: 实际上,信用MSDN上的Rosetta石头:

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

Your code appears to be java. 您的代码似乎是java。

Excel uses Visual basic, for example. 例如,Excel使用Visual Basic。

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

In addition to DaveMac's note above, also keep in mind a couple of points when calling another routine: 除了DaveMac上面的注释之外,在调用另一个例程时还要记住几点:

If you're calling a macro from a routine that resides in the same workbook/addin as that routine, you don't have to use Application.Run. 如果您从与该例程位于同一工作簿/插件中的例程调用宏,则不必使用Application.Run。 You can just call it by using its name: 您可以使用其名称来调用它:

MyMacro

If you're calling a macro that is in a different workbook, then you do need to use Application.Run, but you will also want to use the workbook name where the macro resides, otherwise VBA will not know where it should look for the macro: 如果您正在调用另一个工作簿中的宏,那么您确实需要使用Application.Run,​​但是您还需要使用宏所在的工作簿名称,否则VBA将不知道它应该在哪里查找宏:

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

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

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