从Excel工作簿VSTO调用Excel加载项功能 [英] Call Excel Add-In function from Excel workbook VSTO

查看:472
本文介绍了从Excel工作簿VSTO调用Excel加载项功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我解释一下,我想将其称为Excel Workbook 2007解决方案文件背后代码中的Excel加载项方法。而且我不想使用VBA宏。

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.

请帮助,谢谢!

谢谢,
Mrinal Jaiswal

Thanks, Mrinal Jaiswal

推荐答案

经过大量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

这篇关于从Excel工作簿VSTO调用Excel加载项功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆