2016-01-19 55 views
0

我可以使用VBA或C#來完成此任務。如何以編程方式將郵件項目從目錄文件夾移動到MS Outlook?

問題:存儲在目錄文件夾中(例如: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 
+0

這是否需要通過編程完成 - 你能代替只是將文件從那裏」的文件夾拖放到Outlook重新儲存? –

+0

感謝您的建議。在這種情況下,拖放+拖放不實用。郵件項目被創建並存儲在網絡驅動器中,因此將驅動器中的文件拖放到MS Outlook草稿文件夾時性能很差。第二次從文件夾拖放到「草稿」文件夾時,一次僅限於7個郵件項目。因此,在代碼中這樣做是我理想的解決方案,因爲每次執行此任務時,都可以節省員工拖放數分鐘的時間。我會測試你的Cimperiali解決方案! Tim,完美, –

回答

1

從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 
+0

「OutApp.CreateItemFromTemplate」方法是我需要的。感謝分享! –

相關問題