3

I've created an add-in for outlook 2010. I have a ribbon that has a button on it. When you click that button, I want it to call a procedure in the ThisAddIn.vb.

There are two files: ThisAddin.vb and Ribbon.vb.

I've tried several things to no avail. I've also set all the procedures to public.

Call Testing123()

Call ThisAddIn.Testing123()

Etc

How do I properly call this procedure?

****Ribbon1.vb****
Imports Microsoft.Office.Tools.Ribbon


Public Class MyOutlookTab

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click

        Call Testing123()

    End Sub

End Class


***ThisAddIn.vb***
Public Class ThisAddIn

    Public Sub Testing123()
        System.Windows.Forms.MessageBox.Show("It Works!")

    End Sub

End Class
malt_man
  • 403
  • 1
  • 6
  • 21

3 Answers3

4

The problem is that you are trying to reference class methods without creating a class.

You have three options to make this work:

1) Convert ThisAddIn to a Module. Then there won't be any issues accessing the Testing123 method as you currently have it.

2) Convert ThisAddin.Testing123 to a Shared method, i.e.:

Public Shared Sub Testing123()

Then you would access as follows:

Call ThisAddin.Testing123()

3) Create an instance of the ThisAddIn class prior to using its methods:

Dim oAddIn As New ThisAddIn
Call oAddIn.Testing123()

Update

It appears that addins are treated differently that standard classes.

This MSDN article contains specific implementation guidance for accessing AddIn functionality from other types of solutions.

Based on this article, you need to take a couple of additional steps:

1) Create an interface to expose the functionality from your AddIn:

<ComVisible(True)> _
Public Interface IAddInUtilities
    Sub Testing123()
End Interface

2) Add a utilities class to your addin project:

<ComVisible(True)> _
<ClassInterface(ClassInterfaceType.None)> _
Public Class AddInUtilities
    Implements IAddInUtilities

    Public Sub Testing123() Implements IAddInUtilities.Testing123
        System.Windows.Forms.MessageBox.Show("It Works!")
    End Sub
End Class

3) Add the following to ThisAddIn to expose the utilities to external callers:

Private utilities As AddInUtilities

Protected Overrides Function RequestComAddInAutomationService() As Object
    If utilities Is Nothing Then
        utilities = New AddInUtilities()
    End If
    Return utilities
End Function

4) I am a little unclear on the exact syntax needed for the last step since I don't have automation installed in office, but you will need to do something along these lines:

' OutlookTest should be changed to the name of the project ThisAddIn is in
Dim addIn As Office.COMAddIn = Globals.ThisAddIn.Application.COMAddIns.Item("OutlookTest")
Dim utilities As OutlookTest.IAddInUtilities = TryCast( _
    addIn.Object, OutlookTest.IAddInUtilities)
utilities.Testing123()
competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • ok, I tried option 2 and I was able to recognize testing123 within the MyOutlookTab class but... When I converted the sub to shared it caused other issues with the code (would not recognize me.application.createItem... which is used but I did not include for simplicity sake) – malt_man Dec 12 '11 at 22:49
  • Then I tried option 3 and this seems like its gonna work because it was able to find the procedure but I get an error. "Argument not specified for parameter 'Factory' of 'Public sub New(factory as Microsoft.blahblahblha)'. Never seen this one before. Thanks for your help, I think I'm getting closer – malt_man Dec 12 '11 at 22:52
  • @user7801: Based on that additional info, option 3 may be your best bet. – competent_tech Dec 12 '11 at 22:52
  • ok, for option 3 it wanted me to create a "Sub New" procedure because I'm obviously trying to call it. What do I have to put in it? – malt_man Dec 12 '11 at 22:55
  • @user7801: You shouldn't need to put anything in it, but you could, for safety sake, put `MyBase.New()`. – competent_tech Dec 12 '11 at 22:58
  • I tried MyBase.New() as well as MyClass.New() and it gives me the same error about the Factory of Public sub new. If I don't put anything in the Sub New, the error is: First statement of this 'Sub New' must be a call to 'MyBase.New' or 'MyClass.New' because base class 'Microsoft.Office.Tools.Outlook.OutlookAddInBase' of 'Update_Emailer.ThisAddIn' does not have an accessible 'Sub New' that can be called with no arguments. Again, thanks for your help, I've bee racking my brain all day on this one. – malt_man Dec 12 '11 at 23:07
  • http://msdn.microsoft.com/en-us/library/707s02fy.aspx says to try MyBase.New(Nothing).... that didn't work either. hmm need to dig a little deeper – malt_man Dec 12 '11 at 23:16
  • Can you show the full error message that you get so I can see the full factory name it is expecting? That might help. – competent_tech Dec 12 '11 at 23:19
  • This is the error I get in the Sub New() procedure: First statement of this 'Sub New' must be a call to 'MyBase.New' or 'MyClass.New' because base class 'Microsoft.Office.Tools.Outlook.OutlookAddInBase' of 'Update_Emailer.ThisAddIn' does not have an accessible 'Sub New' that can be called with no arguments. – malt_man Dec 13 '11 at 03:03
  • @malt_man: Thank your for the exception; it shows that you are creating an actual addin as opposed to just having the class named that way. Unfortunately, this makes it more complex to implement. I have updated the answer with a link to the appropriate MSDN documentation and the steps I think you need to take to make it work. – competent_tech Dec 13 '11 at 06:14
  • Thanks for taking the time to look into it further. However I did find a simple solution, see comment. It's frustrating that the addin si so different. Thanks – malt_man Dec 14 '11 at 04:36
2

Thanks for everyones comments but I found the solution in an example here: http://msdn.microsoft.com/en-us/library/ee620548.aspx where they talk about adding a ribbon to the meeting request (2/3's of the way down).

It's actually quite simple. You call the procedure using the "Global"

Globals.ThisAddIn.Testing123()

Nothing else is needed.

malt_man
  • 403
  • 1
  • 6
  • 21
1

You have to create a new instance of the class before you can call it in vb.net!

So something like should allow you to call it..

Public Class MyOutlookTab

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
        Dim testing As New ThisAddIn()
        Call testing.Testing123()

    End Sub

End Class
Standage
  • 1,517
  • 7
  • 22
  • 41