Hi David,
The URLs that Cindy posted are really great readings for this topic
(exposing objects in COM Add-ins for VBA consumers).
Here let me show you a simple example - suppose you're using the C# & C++
add-in shim loader according to your previous posts.
First, you need to create a class that will be called by the VBA macros:
/// <summary>
/// Interface for AddInClass
/// </summary>
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IAddInClass
{
string GetAsmInfo();
}
/// <summary>
/// The implementation of IAddInClass interface.
/// </summary>
[ComVisible(true), ClassInterface(ClassInterfaceType.None)]
public class AddInClass : StandardOleMarshalObject, IAddInClass
{
public string GetAsmInfo()
{
return Assembly.GetExecutingAssembly().ToString();
}
}
Now we have the AddInClass, next is to set an ref to the instance of the
class to the COMAddIn.Object property.
In your Add-in's OnConnection method (on IDTExtensibility2 interface):
public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
......
// cast the addInInst to the COMAddIn interface, set the ref to
AddInClass instance to the Object property.
((Microsoft.Office.Core.COMAddIn)addInInst).Object = new AddInClass();
}
After rebuilding & installing your add-in, you can now write the VBA code
in your Office application:
Sub Test()
Dim addIn As COMAddIn
Dim automationObject As Object
' ExposeToVBA.Connect is the ProgId defined in the Add-in.
Set addIn = Application.COMAddIns("ExposeToVBA.Connect")
' Access the Object property, get the AddInClass instance.
Set automationObject = addIn.Object
' Call the GetAsmInfo method.
MsgBox automationObject.GetAsmInfo
End Sub
That is the simplest sample for exposing an object to the VBA macro from
within the managed COM Add-in.
If you're using VSTO, the only difference is not to use the OnConnection,
but to override the RequestComAddInAutomationService method.
Please let me know if this works for you. Any questions please also let me
know.
Thanks,
Jie Wang
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.