【问题标题】:Save Outlook Mailitem to local folder将 Outlook Mailitem 保存到本地文件夹
【发布时间】:2021-06-16 03:38:04
【问题描述】:

以下代码可以满足我的所有需求:提取电子邮件、保存附件、提取文件 除了将原始电子邮件保存到文件夹 fDest。我似乎看不到解决方案。

这似乎是有问题的行,因为它不会保存电子邮件: "mi.SaveAs fDest2, olMSG"

Sub SaveAttachments()
    Dim ol As Outlook.Application
    Dim ns As Outlook.Namespace
    Dim fol As Outlook.Folder
    Dim i As Object
    Dim mi As Outlook.MailItem
    Dim at As Outlook.Attachment
    Dim Inbox As MAPIFolder
    Dim strDate As String
    Dim oApp As Object
    Dim fDest As Variant
    Dim j As Variant
    Dim sh As String
    Dim FileDialog As FileDialog
    Dim Tracker As Workbook
    Dim fSheet As Sheets
    Dim LastRow As Long
    Dim strFilePath
    Dim fTracker As Workbook
               
    strDate = InputBox("Enter Date in format dd-Mmm-yyyy", "User Date", Format(Now(), "dd-Mmm-yyyy"))
    strFilePath = "\\namdfs\CARDS\MWD\GROUPS\GCM_NAM\2021\05 May\"
    fTrackerName = "Inquiry.Tracker.SWPA.Violations.May.2021.xlsx" '

    On Error Resume Next
        Set fTracker = Workbooks(fTrackerName)
        'If Err Then Set fTracker = Workbooks.Open(strFilePath & fTrackerName)
    On Error GoTo 0
        'Windows(fTrackerName).Activate
      
    Set ol = New Outlook.Application
    Set ns = ol.GetNamespace("MAPI")
    Set fol = ns.Folders("GCMNamLogs").Folders("Inbox")
        
    fDest = "C:\Users\jb76991\Desktop\Violations_Emails\"
    fUser = UCase(Environ("username")) & ":" & Chr(10) & Now()
        
    For Each i In fol.Items.Restrict("@SQL=urn:schemas:httpmail:subject LIKE '%" & strDate & "%'")
        'Debug.Print fDest & i & ".msg"
        If i.Class = olMail Then
            Set mi = i
            fDest2 = fDest & mi.Subject & ".msg"
            mi.SaveAs fDest2, olMSG
            For Each at In mi.Attachments
                'do something with attachments but i've commented it out
            Next at
        End If
    Next i
    MsgBox ("Completed")
    
End Sub

谁能告诉我如何保存被过滤的原始电子邮件?

【问题讨论】:

  • mi.Subject 中是否有不允许作为文件名一部分的字符?您看到的确切错误消息是什么?看看这里的函数ReplaceIllegalChar - stackoverflow.com/questions/53422235/…
  • Tim Williams,没有非法字符。我正在使用已经存在的相同名称(或尝试使用)。 "运行时错误'-2147286788 (800300fc)':操作失败。
  • @Ted 啊啊啊!我错了!是的,有无效字符。一旦我纠正了它们,它就可以正常工作了......

标签: excel vba outlook


【解决方案1】:

您必须确保文件名中没有无效字符。请参阅What characters are forbidden in Windows and Linux directory names? 了解更多信息。因此,我建议在将任何内容传递给 SaveAs 方法之前使用 VBA 中可用的 Replace 方法。

另一点是您需要为每封电子邮件指定唯一的文件名。确保生成的文件名对于文件夹是唯一的。

【讨论】:

  • 我已经调试了对象和 mailitem 对象名称,并且没有无效字符。在处理邮件对象方面,我承认我很穷。
  • 传递给SaveAs方法的实际参数是什么?
  • 啊啊啊!我错了!是的,有无效字符。一旦我纠正了它们,它就可以正常工作了......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-24
  • 2013-12-23
  • 2018-07-10
  • 1970-01-01
  • 2020-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多