Rss & SiteMap

Foxtable(狐表) http://www.foxtable.com

新一代数据库软件,完美融合Access、Foxpro、Excel、vb.net之优势,人人都能掌握的快速软件开发工具!
共6 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

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

1楼
ljh29206 发表于:2024/6/28 11:44:00
以下 为 代码  从 c# 转换到 vb.net

如何把 该内容 变为  foxtable 可用的全局代码 求指点 ,谢谢 老板

Imports System
Imports System.Linq
Imports Microsoft.Office.Interop.Outlook
Imports System.Collections.Generic

Namespace EmailReceiver
    Class Program
        Shared outlookApp As ApplicationClass = New Microsoft.Office.Interop.Outlook.ApplicationClass()
        Shared ns As [NameSpace]

        Private Shared Sub Main(ByVal args As String())
            Console.WriteLine("start to monitor new emails")
            ns = outlookApp.GetNamespace("MAPI")
            outlookApp.NewMailEx += New ApplicationEvents_11_NewMailExEventHandler(AddressOf outlookApp_NewMailEx)
            outlookApp.NewMail += New ApplicationEvents_11_NewMailEventHandler(AddressOf outlookApp_NewMail)

            While True
            End While
        End Sub

        Private Shared Sub outlookApp_NewMail()
            Console.WriteLine("a new message comes: new email")
        End Sub

        Private Shared Sub outlookApp_NewMailEx(ByVal EntryIDCollection As String)
            Console.WriteLine("a new message comes")
            AnalyzeNewItem(EntryIDCollection)
        End Sub

        Private Shared Sub AnalyzeNewItem(ByVal entry As String)
            Dim inbox = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox)
            Dim allmails As List(Of MailItem) = New List(Of MailItem)()

            For Each item In inbox.Items

                If TypeOf item Is MailItem Then
                    Dim mail = TryCast(item, 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
                Console.WriteLine(latestMailItem.Subject)
                Console.WriteLine(latestMailItem.[To])
                Console.WriteLine(latestMailItem.SenderName)
                Console.WriteLine(latestMailItem.ReceivedTime)
                Console.WriteLine(latestMailItem.Body)
            End If
        End Sub
    End Class
End Namespace
2楼
baicaocao 发表于:2024/6/28 13:16:00
Microsoft Visual Studio Enterprise 2022 中,有个扩展里面有个插件,可以转换,code converter(vb-c#)
3楼
有点蓝 发表于: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

4楼
ljh29206 发表于:2024/6/28 15:03:00
这个代码 是 已经从 c# 转换为 vb 的了

篮板  我用 你的代码 放进去  全局代码 里面 

会提示   未定义  name space

sheared ns as [namespace]

可否帮忙再看看呢 ,  foxtable 可以这样定义命名空间的?  不懂怎么搞 求助
5楼
有点蓝 发表于:2024/6/28 15:05:00
这个 [namespace]应该不是命名空间,而是一个类型。

改为这样试试
sheared ns as object
6楼
有点蓝 发表于: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
共6 条记录, 每页显示 10 条, 页签: [1]

Copyright © 2000 - 2018 foxtable.com Tel: 4000-810-820 粤ICP备11091905号

Powered By Dvbbs Version 8.3.0
Processed in .02930 s, 2 queries.