Exposing C# VSTO Add-In to VBA

J

james

I've looked at MS's and Whitechapel's code, but I can't seem to get it
right. I was hoping someone could see what I am doing wrong..

----- In the ribbon class:

namespace ARisk_Reporting
{
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IExposedMethods
{
void RefreshAlllPivots(Excel.Workbook wkb);

void RefreshThisPivot(Excel.PivotTable pvt);
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public partial class ARISK_Ribbon : OfficeRibbon, IExposedMethods
{


----- Class includes two public methods:

public void RefreshAlllPivots(Excel.Workbook wkb)
{

public void RefreshThisPivot(Excel.PivotTable pvt)
{


----- ThisAddIn code:

private ARISK_Ribbon ribbon;
protected override object RequestComAddInAutomationService()
{
if (ribbon == null)
{
ribbon = new ARISK_Ribbon();
}
return ribbon;
}
 
J

james

When i try to execute the following in VBA, the Object equals nothing,
so it throws an erro:

Sub ExposedAddInTest()

Dim addIn As COMAddIn
Dim automationObject As Object
Set addIn = Application.COMAddIns("ARisk_Reporting")
Set automationObject = addIn.Object

''throws error here, since addIn.Object = Nothing
automationObject.RefreshAllPivots

End Sub


The code looks OK to me, what's your problem?

--
Ken Slovak
[MVP - Outlook]http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.http://www.slovaktech.com/products.htm




I've looked at MS's and Whitechapel's code, but I can't seem to get it
right.  I was hoping someone could see what I am doing wrong..
----- In the ribbon class:
namespace ARisk_Reporting
{
   [ComVisible(true)]
   [InterfaceType(ComInterfaceType.InterfaceIsDual)]
   public interface IExposedMethods
   {
       void RefreshAlllPivots(Excel.Workbook wkb);
       void RefreshThisPivot(Excel.PivotTable pvt);
   }
   [ComVisible(true)]
   [ClassInterface(ClassInterfaceType.None)]
   public partial class ARISK_Ribbon : OfficeRibbon, IExposedMethods
   {
----- Class includes two public methods:
       public void RefreshAlllPivots(Excel.Workbook wkb)
       {
       public void RefreshThisPivot(Excel.PivotTable pvt)
       {
----- ThisAddIn code:
       private ARISK_Ribbon ribbon;
       protected override object RequestComAddInAutomationService()
       {
           if (ribbon == null)
           {
               ribbon = new ARISK_Ribbon();
           }
           return ribbon;
       }- Hide quoted text -

- Show quoted text -
 
K

Ken Slovak - [MVP - Outlook]

You're trying this from the Outlook VBA project?

That's pretty normal, you have to phrase your call a bit differently:

Dim addIn As COMAddIn
Set addIn = Application.COMAddIns.Item("This should be the addin ProgID
here")
addIn.Object.RefreshAllPivots ' need to call with a workbook object

Don't get Object as a separate object. In this case multiple dot operators
are good.





When i try to execute the following in VBA, the Object equals nothing,
so it throws an erro:

Sub ExposedAddInTest()

Dim addIn As COMAddIn
Dim automationObject As Object
Set addIn = Application.COMAddIns("ARisk_Reporting")
Set automationObject = addIn.Object

''throws error here, since addIn.Object = Nothing
automationObject.RefreshAllPivots

End Sub
 
J

james

No, running from Excel VBA. Tried the following, but still errors as
"Object doesn't support this property or method":

Sub ExposedAddInTest()

Dim addIn As COMAddIn
Dim automationObject As Object

Set addIn = Application.COMAddIns.Item("ARisk_Reporting")
addIn.Object.RefreshAllPivots ThisWorkbook

End Sub
 
K

Ken Slovak - [MVP - Outlook]

The intrinsic Application object would apply to Excel in that case, is that
what you are looking for?

Is "ARisk_Reporting" the actual ProgID for your addin? You aren't supplying
the name of the externally exposed classes or interfaces you know, you need
to use the actual ProgID.

Is addIn a valid object, what are the values if you use debug to check the
values of addIn.Guid or addIn.ProgID? Are they as expected?





No, running from Excel VBA. Tried the following, but still errors as
"Object doesn't support this property or method":

Sub ExposedAddInTest()

Dim addIn As COMAddIn
Dim automationObject As Object

Set addIn = Application.COMAddIns.Item("ARisk_Reporting")
addIn.Object.RefreshAllPivots ThisWorkbook

End Sub
 
J

james

From the immediate window it does seem as if the ProgId is correct:

?Application.COMAddIns.Item(1).Description
ARisk_Reporting
?Application.COMAddIns.Item(1).ProgId
ARisk_Reporting
?Application.COMAddIns.Item(1).Guid
{60E1F3D8-C762-4112-B888-1F1ACB0674F4}
 
K

Ken Slovak - [MVP - Outlook]

Hmm. Maybe you can spot the differences in how you're doing it and how I do
it. This is from a C# VSTO Outlook addin template I used in my Outlook 2007
programming book. It definitely works.

The following init code is run in ThisAddin_Startup()

try

{

// get the ProgID of the addin to use later with buttons/toolbars

AssemblyName thisAssemblyName = null;

thisAssemblyName = Assembly.GetExecutingAssembly().GetName();

m_ProgID = thisAssemblyName.Name; //ProgID for the addin

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}


try

{

AddinObject = new AutomationObject(this, ref m_InspWrap, m_ProgID);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

Then this code is in the ThisAddIn class:

private System.Collections.SortedList m_InspWrap = null;

private string m_ProgID = "";

public static AutomationObject AddinObject = null;

private AddinUtilities addinUtilities;

protected override object RequestComAddInAutomationService()

{

if (addinUtilities == null)

{

addinUtilities = new AddinUtilities();

}

return addinUtilities;

}

Then after the class:

[ComVisible(true)]

[InterfaceType(ComInterfaceType.InterfaceIsDual)]

public interface IAddinUtilities

{

void CalledFromOutside();

}

[ComVisible(true)]

[ClassInterface(ClassInterfaceType.None)]

public class AddinUtilities : IAddinUtilities

{

// Demonstrates a method that can be called from outside the addin.

// This technique can be used to call functions and to read/write
properties.

public void CalledFromOutside()

{

AutomationObject myAddinObject = ThisAddIn.AddinObject;

string Count = myAddinObject.ExplCount();

string ID = myAddinObject.ProgID();

//MessageBox.Show("This is a test of an outside call to the COM addin");

MessageBox.Show("There are currently " + Count + " Explorers open.");

}

}

Then I have this as my AutomationObject class:

public class AutomationObject

{

private Outlook.Application m_application;

private System.Collections.SortedList m_Wrapper; //Inspector wrapper

private string m_ProgID;

// class constructor

public AutomationObject(ThisAddIn application, ref
System.Collections.SortedList Wrapper, string ProgID)

{

m_application = application.Application;

m_Wrapper = Wrapper;

m_ProgID = ProgID;

}

public string ProgID()

{

return m_ProgID;

}

public string ExplCount()

{

string RetVal = m_application.Explorers.Count.ToString();

return RetVal;

}

public System.Collections.SortedList InspectorWrapper()

{

return m_Wrapper;

}

public Outlook.Application App()

{

return m_application;

}

}

From the outside I'd call the code this way:

Dim oAddin As Office.COMAddIn
Set oAddin = Application.COMAddIns.Item("VSTO_CSOutlookTemplate") ' my addin
ProgID
If Not (oAddin Is Nothing) Then
oAddin.Object.CalledFromOutside
End If




From the immediate window it does seem as if the ProgId is correct:

?Application.COMAddIns.Item(1).Description
ARisk_Reporting
?Application.COMAddIns.Item(1).ProgId
ARisk_Reporting
?Application.COMAddIns.Item(1).Guid
{60E1F3D8-C762-4112-B888-1F1ACB0674F4}
 

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