如何以编程方式将邮件项目从目录文件夹移动到 MS Outlook

本文关键字:文件夹 移动 Outlook MS 项目 编程 方式 | 更新日期: 2023-09-27 18:30:56

我可以使用VBA或C#来完成此任务。

问题:我在目录文件夹(例如:C:'temp)中存储了 100+ 个邮件项目(.msg、包含主题行、收件人收件人和附件的未发送电子邮件)。

如何循环访问邮件项目并将它们发送到 MS Outlook 2010 中的文件夹?

我尝试在 VBA 中使用 FileSystemObject,我能够遍历目录中的"文件"。但是,我无法将文件转换为MailItem以将每个文件移动到MS Outlook。

我的尝试:

    Public Sub Move_MailItems_To_Outlook_From_Directory_Folder()
    Dim directoryPath       As String
    Dim directoryFolder     As Object
    Dim aFile               As File
    Dim fso                 As New FileSystemObject
    Dim outlookApp          As Outlook.NameSpace
    Dim emailFolder         As Outlook.Folder
    directoryPath = "''Client'F$'temp'"
    Set directoryFolder = fso.GetFolder(directoryPath)
    Set outlookApp = Application.GetNamespace("MAPI")
    ' Iterate thru files and add to MS Outlook "Drafts" folder.
    For Each aFile In directoryFolder.Files
        ' !!!! "ERROR: One or more parameter values are not valid." !!!
        '
        ' I don't think VBA provides a means to cast a File object as a MailItem
        ' object, which is what I imagine the Items.Add method expects as a parameter
        ' value.
        outlookApp.GetDefaultFolder(olFolderDrafts).Items.Add aFile
    Next
End Sub

如何以编程方式将邮件项目从目录文件夹移动到 MS Outlook

来自Cimperiali的回答:http://forums.codeguru.com/showthread.php?287203-vb6-how-to-open-an-outlook-email-that-s-located-on-the-harddrive&highlight=email

Private OutApp As Outlook.Application
Private Function getMailMessage(ByVal FileName As String) As String
    Dim outMsg As Object
    Dim outDraftFolder As MAPIFolder
    If Dir$(FileName) = "" Then
        'nothing to read
        getMailMessage = "File " & FileName & " not found"
        Exit Function
    End If
    If OutApp Is Nothing Then
        Set OutApp = New Outlook.Application
    End If
    Dim outFold As Outlook.Folders
    'get Draft folder of outlook
    Set outDraftFolder = OutApp.GetNamespace("MAPI").GetDefaultFolder(olFolderDrafts)
    'load message as draft - it may be something else than a mailitem...
    Set outMsg = OutApp.CreateItemFromTemplate(FileName)
    'check the type:
    Dim sText As String
    If TypeOf outMsg Is Outlook.MailItem Then
        With outMsg
            sText = "A mailItem:"
            sText = sText & vbCrLf & "sender =" & .SenderName
            sText = sText & vbCrLf & "Received = " & .ReceivedTime
            sText = sText & vbCrLf & "Created = " & .CreationTime
            sText = sText & vbCrLf & "subject = " & .Subject
            sText = sText & vbCrLf & "Body:" & vbCrLf
            sText = sText & vbCrLf & .Body
         End With
    ElseIf TypeOf outMsg Is Outlook.ContactItem Then
         With outMsg
            sText = "A ContactItem:"
            sText = sText & vbCrLf & "Created = " & .CreationTime
            sText = sText & vbCrLf & "subject = " & .Subject
            sText = sText & vbCrLf & "NickName=" & .NickName
            sText = sText & vbCrLf & "Email: " & .Email1Address
            sText = sText & vbCrLf & "Company Name: " & .CompanyName
            sText = sText & vbCrLf & "Profession: " & .Profession
            sText = sText & vbCrLf
            sText = sText & vbCrLf & "Body:" & vbCrLf
            sText = sText & vbCrLf & .Body
         End With
    ElseIf TypeOf outMsg Is Outlook.AppointmentItem Then
        With outMsg
            sText = "An AppointmentItem:"
            sText = sText & vbCrLf & "Created = " & .CreationTime
            sText = sText & vbCrLf & "subject = " & .Subject
            sText = sText & vbCrLf & "Conversation Topic=" & .ConversationTopic
            sText = sText & vbCrLf & "Importance: " & .Importance
            sText = sText & vbCrLf & "Duration: " & .Duration
            sText = sText & vbCrLf & "Last Modification time: " & .LastModificationTime
            sText = sText & vbCrLf
            sText = sText & vbCrLf & "Body:" & vbCrLf
            sText = sText & vbCrLf & .Body
        End With
    ElseIf TypeOf outMsg Is Outlook.MeetingItem Then
        Dim mx As Outlook.MeetingItem
        With mx
            sText = "A MeetingItem:"
            sText = sText & vbCrLf & "Created = " & .CreationTime
            sText = sText & vbCrLf & "subject = " & .Subject
            sText = sText & vbCrLf & "Conversation Topic=" & .ConversationTopic
            sText = sText & vbCrLf & "Importance: " & .Importance
            sText = sText & vbCrLf & "Expiry Time: " & .ExpiryTime
            sText = sText & vbCrLf & "Last Modification time: " & .LastModificationTime
            sText = sText & vbCrLf
            sText = sText & vbCrLf & "Body:" & vbCrLf
            sText = sText & vbCrLf & .Body
        End With
    Else
        sText = "You need to write a bit more of code..."
    End If
    getMailMessage = sText
    Set outMsg = Nothing
    Set outDraftFolder = Nothing
End Function
相关文章: