使用 VB.Net 在 Outlook 资源管理器中获取选定的邮件项目

Get selected mail items in Outlook Explorer with VB.Net

我正在尝试将我放在一起的一些 VBA 代码转换为 Outlook 插件,以避免在安全性降低的环境中不得不 运行 宏。

我知道的 VBA 和 VB6 足以涉足编写我自己使用的东西,这是我第一次涉足 VB.Net。

我刚刚安装了 Visual Studio(社区版),从 VBA 到的转换比我预期的要顺利,这要归功于 IDE 除了一两个问题之外的建议。

其中之一是尝试根据此处提供的代码在 Outlook 中选择项目:https://msdn.microsoft.com/en-us/library/office/ff868001.aspx

Sub GetSelectedItems() 
    Dim myOlExp As Outlook.Explorer 
    Dim myOlSel As Outlook.Selection 
    Dim oMail As Outlook.MailItem 
    Dim x As Integer 

    myOlExp = Application.ActiveExplorer ' => This generates an error
    myOlSel = myOlExp.Selection 
    For x = 1 To myOlSel.Count 
        If myOlSel.Item(x).Class = OlObjectClass.olMail Then 
            Set oMail = myOlSel.Item(x) 
            ' Do Stuff 
        End If 
    Next x 
End Sub 

我无法解决 Application.ActiveExplorer 生成的错误消息。

关于如何将其转换为 VB.Net 的任何建议?我搜索了高低,但我找到的所有示例都是基于 VBA。

SO Question 很接近,但我无法在两种语言之间实现跨越。

经过努力学习,我设法使用等价于解决这个问题:

Imports Microsoft.Office.Interop.Outlook

Sub GetSelectedItems() 
    Dim myOlApp As Outlook.Application = New Outlook.Application  
    Dim myOlExp As Outlook.Explorer = myOlApp.ActiveExplorer
    Dim myOlSel As Outlook.Selection = myOlExp.Selection
    Dim oMail As Outlook.MailItem 
    Dim x As Integer 

    For x = 1 To myOlSel.Count 
        If (TypeOf myOlSel.Item(x).Class is MailItem) Then 
            oMail = myOlSel.Item(x) 
            ' Do Stuff 
        End If 
    Next x 
End Sub