vb.net - 在另一个类中调用过程

标签 vb.net outlook add-in

我已经为 outlook 2010 创建了一个加载项。我有一个带有按钮的功能区。当您单击该按钮时,我希望它调用 ThisAddIn.vb 中的过程。

有两个文件:ThisAddin.vb 和 Ribbon.vb。

我尝试了很多方法都无济于事。我也把所有的程序都公开了。

调用 Testing123()

调用 ThisAddIn.Testing123()

等等

如何正确调用此过程?

****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) 将 ThisAddIn 转换为 Module。那么访问 Testing123 方法就不会出现任何问题,因为您当前拥有它。

2) 将 ThisAddin.Testing123 转换为 Shared 方法,即:

Public Shared Sub Testing123()

然后你会访问如下:

Call ThisAddin.Testing123()

3) 在使用 ThisAddIn 类的方法之前创建一个实例:

Dim oAddIn As New ThisAddIn
Call oAddIn.Testing123()

更新

看来插件的处理方式与标准类不同。

This MSDN article包含从其他类型的解决方案访问插件功能的具体实现指南。

根据本文,您需要执行几个额外的步骤:

1) 创建一个接口(interface)来公开您的插件的功能:

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

2) 将实用程序类添加到您的插件项目:

<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) 将以下内容添加到 ThisAddIn 以将实用程序公开给外部调用者:

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) 我不太清楚最后一步所需的确切语法,因为我没有在 office 中安装自动化,但你需要按照以下几行做一些事情:

' 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()

关于vb.net - 在另一个类中调用过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8480680/

相关文章:

java - 如何从java打开outlook邮件,并使用html代码预先填充正文

outlook - 签名/证书 VSTO Outlook 插件

c# - 如何使用wordProcessingDocument加载wordopenxml(字符串形式)

.net - 使用 VB.NET 从字符串中检测 IFormatProvider 或 CultureInfo

outlook - 开始使用MAPI的最佳方法是什么?

c# - 如何使用类似插件的架构来扩展 WPF 应用程序?

visual-studio-2008 - 使用 Qt 插件 1.1.4 启动时,Visual Studio 2008 崩溃

database - 范围变量隐藏封闭 block LINQ 中的变量

vb.net - 如何停止将已知流派转换为 id3v2 标签中的数字?

c# - HTML 在 outlook 2007 电子邮件中显示不正确?