Hi All,
After doing much googleing i m able to answer it myself now.
Please follow the following steps,
- 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
- 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
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.
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