且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从Excel工作簿VSTO调用Excel加载项功能

更新时间:2023-12-05 20:23:10

经过大量Google搜索,我现在就可以自己回答了。

After doing much googleing i m able to answer it myself now.

请按照以下步骤操作,


  1. 声明一个带有要从工作簿公开的函数的接口,并设置其ComVisible属性确实,


公共接口ICallMe
Sub MyFunction()
终端接口

Public Interface ICallMe Sub MyFunction() End Interface


  1. 现在创建一个实现此接口的类,并将其ComVisible属性设置为true,并将classinterface属性设置为None,类似于



公共类AddInUtilities
实现ICallMe

Public Class AddInUtilities Implements ICallMe

Public Sub MyFunction() Implements ICallMe.MyFunction
    Dim activeWorksheet As Excel.Worksheet = Globals.ThisAddIn.Application.ActiveSheet

    If activeWorksheet IsNot Nothing Then
        Dim range1 As Excel.Range = activeWorksheet.Range("A1")
        range1.Value2 = "This comes from my Add-In"
    End If
End Sub

结束舱位


  1. 5。使用注册COM互操作选项构建您的外接程序项目。要设置注册COM互操作选项,请转到项目属性,然后转到编译选项卡(在VB.net中),然后将注册COM互操作设置为选中。

  1. 5.Build your add-in project with the Register for COM interop option. To set "Register for Com Interop" option, goto project property and then to "Compile" tab (In VB.net), and set the "Register for COM interop" to check.

现在在excel工作簿项目中,将引用添加到此加载项,并在工作簿的任何事件(例如按钮单击)上,编写以下代码,

Now in your excel workbook project, add the refrence to this add-in and on any event of your workbook like button click, write the following code,

Dim addIn As Office.COMAddIn = Application.COMAddIns.Item( ImportData)

Dim addIn As Office.COMAddIn = Application.COMAddIns.Item("ImportData")

Dim实用程序为ImportData.ICallMe = TryCast(addIn.Object,ImportData.ICallMe)

Dim utilities As ImportData.ICallMe = TryCast(addIn.Object, ImportData.ICallMe)

utilities.MyFunction()

utilities.MyFunction()

您完成:)

唯一要记住的是,不要从工作簿或工作表的启动事件中调用加载项功能。

我希望它对您有用,因为它对我有用。

I hope it helps you all, as it works for me.

谢谢,
Mrinal Jaiswal

Thanks, Mrinal Jaiswal