views:

387

answers:

1

Hi,

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

A: 

Hi All,

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

mickey mouse