问题
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
回答1:
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()
回答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.
回答3:
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
来源:https://stackoverflow.com/questions/8480680/calling-a-procedure-within-another-class