从 Excel 工作簿 VSTO 调用 Excel 加载项函数

发布于 2024-08-17 11:33:49 字数 184 浏览 5 评论 0原文

我想在 Excel Add-In 2007 解决方案中创建一个方法,可以从我的 Excel Workbook 2007 解决方案中调用该方法。

我解释一下,我想从Excel工作簿2007解决方案的代码隐藏文件中调用Excel加载项的方法。我不想使用 VBA 宏。

请帮忙,提前致谢

谢谢, 姆纳尔·杰斯瓦尔

I want to create a Method in Excel Add-In 2007 solution which can be called from my Excel Workbook 2007 solution.

I explain, I want to call a method of Excel Add-In from code behind file of excel workbook 2007 solution. And I dont want to use VBA Macros.

Please help, thanks in advance

Thanks,
Mrinal Jaiswal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

渔村楼浪 2024-08-24 11:33:49

经过大量谷歌搜索后,我现在可以自己回答了。

请按照以下步骤操作,

  1. 声明一个包含要从工作簿中公开的函数的接口,并将其 ComVisible 属性设置为 true,

公共接口ICallMe
子我的函数()
End Interface

  1. 现在创建一个实现该接口的类,并将其 ComVisible 属性设置为 true,并将 classinterface 属性设置为 None,有些像这样,

公共类 AddInUtilities
实现 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

End Class

  1. 5. 使用“注册 COM 互操作”选项构建您的加载项项目。要设置“Register for Com Interop”选项,请转到项目属性,然后转到“Compile”选项卡(在 VB.net 中),然后设置“Register for COM interop”进行检查。

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

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

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

utilities.MyFunction()

你已经完成了:)

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

我希望它对大家有所帮助,因为它对我有用。

谢谢,
姆纳尔·杰斯瓦尔

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

Please follow the following steps,

  1. Declare an interface with functions you want to expose from your workbook and set its ComVisible attribute to true,

Public Interface ICallMe
Sub MyFunction()
End Interface

  1. Now create a class which implements this interface and set its ComVisible attribute to true, along with classinterface attribute to None, some what like this,

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

End Class

  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.

  2. 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 utilities As ImportData.ICallMe = TryCast(addIn.Object, ImportData.ICallMe)

utilities.MyFunction()

You are done :)

Only thing to remeber is that do not call the add-in function from startup event of your workbook or sheet.

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

Thanks,
Mrinal Jaiswal

谁对谁错谁最难过 2024-08-24 11:33:49

非常感谢对我有用。现在已经为此苦苦挣扎了2天。
我想使用 Excel Addin(用户定义的函数)在 Office 2016 中动态创建一个新工作簿。

只是我如何使其工作的示例:

public static void printToExcel(String writeToCell)
{
    Workbook xlWorkbook = Globals.ThisAddIn.Application.Workbooks.Add();
    Worksheet xlWorkSheet = xlWorkbook.ActiveSheet;
    xlWorkSheet.Name = "testSheet";
    xlWorkSheet.Cells[1, 1] = writeToCell;
}

Thanks a lot that worked for me. Was struggling with it for 2 days now.
I wanted to create a new workbook on the fly in Office 2016 with my Excel Addin, user defined function.

Just a sample how I made it work :

public static void printToExcel(String writeToCell)
{
    Workbook xlWorkbook = Globals.ThisAddIn.Application.Workbooks.Add();
    Worksheet xlWorkSheet = xlWorkbook.ActiveSheet;
    xlWorkSheet.Name = "testSheet";
    xlWorkSheet.Cells[1, 1] = writeToCell;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文