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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8480680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:47:55  来源:igfitidea点击:

Calling a procedure within another class

vb.netoutlookadd-in

提问by malt_man

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.

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

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

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

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

我尝试了几件事都无济于事。我还把所有的程序都公开了。

Call Testing123()

调用测试123()

Call ThisAddIn.Testing123()

调用 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

采纳答案by malt_man

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

感谢大家的评论,但我在这里的一个例子中找到了解决方案:http: //msdn.microsoft.com/en-us/library/ee620548.aspx,他们谈论在会议请求中添加功能区(2/3一路下来)。

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

其实很简单。您使用“全局”调用该过程

Globals.ThisAddIn.Testing123()

Nothing else is needed.

不需要其他任何东西。

回答by competent_tech

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 ThisAddInto a Module. Then there won't be any issues accessing the Testing123method as you currently have it.

1) 转换ThisAddInModule. 那么访问该Testing123方法不会有任何问题,因为您目前拥有它。

2) Convert ThisAddin.Testing123to a Sharedmethod, i.e.:

2)转换ThisAddin.Testing123Shared方法,即:

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:

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

Dim oAddIn As New ThisAddIn
Call oAddIn.Testing123()

Update

更新

It appears that addins are treated differently that standard classes.

似乎插件的处理方式与标准类不同。

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

这篇 MSDN 文章包含从其他类型的解决方案访问加载项功能的具体实施指南。

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

根据本文,您需要采取一些额外的步骤:

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

1) 创建一个接口以从您的 AddIn 公开功能:

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

2) Add a utilities class to your addin project:

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) Add the following to ThisAddIn to expose the utilities to external callers:

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) 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:

4)我对最后一步所需的确切语法有点不清楚,因为我没有在办公室安装自动化,但您需要按照以下方式做一些事情:

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

回答by Standage

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

您必须先创建该类的新实例,然后才能在 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