Foxtable(狐表)用户栏目专家坐堂 → vb.net 代码转换请教


  共有118人关注过本帖树形打印复制链接

主题:vb.net 代码转换请教

帅哥,在线噢!
有点蓝
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:107836 积分:548531 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2024/6/28 13:41:00 [显示全部帖子]

大概

Public Class Program
    Shared outlookApp As New Microsoft.Office.Interop.Outlook.ApplicationClass()
    Shared ns As [NameSpace]
    
    Public Shared Sub Main()
        msgbox("start to monitor new emails")
        ns = outlookApp.GetNamespace("MAPI")
        AddHandler outlookApp.NewMailEx , AddressOf outlookApp_NewMailEx
        AddHandler outlookApp.NewMail , AddressOf outlookApp_NewMail
        
        While True
        End While
    End Sub
    
    Private Shared Sub outlookApp_NewMail()
        msgbox("a new message comes: new email")
    End Sub
    
    Private Shared Sub outlookApp_NewMailEx(ByVal EntryIDCollection As String)
        msgbox("a new message comes")
        AnalyzeNewItem(EntryIDCollection)
    End Sub
    
    Private Shared Sub AnalyzeNewItem(ByVal entry As String)
        Dim inbox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox)
        Dim allmails As List(Of Microsoft.Office.Interop.Outlook.MailItem) = New List(Of Microsoft.Office.Interop.Outlook.MailItem)()
        
        For Each item In inbox.Items
            
            If TypeOf item Is Microsoft.Office.Interop.Outlook.MailItem Then
                Dim mail = TryCast(item, Microsoft.Office.Interop.Outlook.MailItem)
                allmails.Add(mail)
            End If
        Next
        
        Dim latest = allmails.Max(Function(s) s.ReceivedTime)
        Dim latestMailItem = allmails.FirstOrDefault(Function(s) s.ReceivedTime = latest)
        
        If latestMailItem IsNot Nothing Then
            msgbox(latestMailItem.Subject)
            msgbox(latestMailItem.[To])
            msgbox(latestMailItem.SenderName)
            msgbox(latestMailItem.ReceivedTime)
            msgbox(latestMailItem.Body)
        End If
    End Sub
End Class


 回到顶部
帅哥,在线噢!
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:107836 积分:548531 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2024/6/28 15:05:00 [显示全部帖子]

这个 [namespace]应该不是命名空间,而是一个类型。

改为这样试试
sheared ns as object

 回到顶部
帅哥,在线噢!
有点蓝
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:107836 积分:548531 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2024/6/28 15:11:00 [显示全部帖子]

https://learn.microsoft.com/zh-cn/office/vba/api/Outlook.Application.GetNamespace

sheared ns as Microsoft.Office.Interop.Outlook.namespace

 回到顶部