Calling Add In Functions

A

Anbu

Hi,

Can we call a Add In's function from a VBA Macro? Add In could be written using VB 6 or VS .NET

Thanks,
Anbu
 
A

Anbu

Yes, But I could create a VB6 add In or DLL. How to refer to a dotNET add In

Any Idea/suggesions?



Hi,

Can we call a Add In's function from a VBA Macro? Add In could be written using VB 6 or VS .NET
Yes you can

Make sure the function is not static

inside my Word template

Dim word_addin As Object

Private Sub DoInitialisation()
On Error GoTo cancel
If Not initialised Then
Set word_addin = CreateObject("MyNameSpace.AddinObject")
initialised = True
End If

Exit Sub
cancel:
End Sub

Public Sub DoAddinFunction()
On Error GoTo ProcError
DoInitialisation

If initialised Then
word_addin.MyAddinFunction
End If

Exit Sub

ProcError:
MsgBox ("Did not Work ")

End Sub


Thanks,
Anbu

Good Luck
 
U

Uday Takbhate [MSFT]

Hello Anbu,

You can call .Net component from VBA.
Here is the summery of what I did to achieve this.

(1) Created C# class library project which has a class definition as
follows,

using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace TestClassLibraryForExcel
{
[ClassInterface(ClassInterfaceType.AutoDual)]
public class MyFunctions
{
public MyFunctions()
{
}

// this is my user defined function
public double LuckyNumber()
{
return (7);
}

[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type type)
{
Registry.ClassesRoot.CreateSubKey(GetSubKeyName(type));
}

[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type type)
{

Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(type),false);
}

private static string GetSubKeyName(Type type)
{
string s = @"CLSID\{" + type.GUID.ToString().ToUpper() +
@"}\Programmable";
return s;
}
}
}

(2) Gave full trust to the assembly using the .Net framework wizards

(3) Registered the managed assembly for COM Interop in the registry by
using the assembly registration tool (Regasm.exe) with the /TLB and
/CODEBASE options to register the .NET Component for COM Interop and
creates a type library and a COM Callable Wrapper (CCW) Interop assembly.

To know more about the Regasm tool you may refer the following article
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptools/htm
l/cpgrfassemblyregistrationtoolregasmexe.asp

Once a class is registered, any COM client can use it as though the class
were a COM class

You may install the assembly to the global assembly cache using the Global
assembly cache tool
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptools/ht
ml/cpgrfGlobalAssemblyCacheUtilityGacutilexe.asp) if you do not want to use
the /CODEBASE switch to the RegAsm tool.

(5) Created a workbook and activated the Visual basic editor and added the
reference to the TestClassLibraryForExcel from Tools->References menu.

(6) Now, you may create object of TestClassLibraryForExcel same as a normal
COM object.

I hope this helps you!

Regards,
Uday Takbhate [MSFT]
Microsoft Developer Support
--------------------
 
U

Uday Takbhate [MSFT]

Hello Anbu,

You may write a managed add-in as well. The previous code snippet I sent
you explained about calling a managed class library from VBA.

Thanks,
Uday Takbhate [MSFT]
Microsoft Developer Support
--------------------
Date: Fri, 02 Sep 2005 12:04:48 +1000
From: Angelo Fraietta <[email protected]>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4)
Gecko/20030624 Netscape/7.1 (ax)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top