【问题标题】:Saving Outlook email as PDF + Attachments将 Outlook 电子邮件另存为 PDF + 附件
【发布时间】:2015-05-02 23:04:42
【问题描述】:

所以我使用宏来保存收到的邮件(使用收件箱规则和 VBA 代码)。我遇到的问题是,当有多个具有相同名称的电子邮件(以及附件具有相同名称)时,它们将不会保存。 (它们相互覆盖)。

我需要电子邮件和附件都循环遍历 1-10(最多可以有十封同名的电子邮件和附件)。代码如下:

Sub SaveAsMsg(MyMail As MailItem)
' requires reference to Microsoft Scripting Runtime
' \Windows\System32\Scrrun.dll
' Also requires reference to Microsoft Word Object Library
Dim fso As FileSystemObject
Dim strSubject As String
Dim strSaveName As String
Dim blnOverwrite As Boolean
Dim strFolderPath As String
Dim looper As Integer
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim oMail As Outlook.MailItem

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set oMail = olNS.GetItemFromID(strID)

'Get Sender email domain
sendEmailAddr = oMail.SenderEmailAddress
companyDomain = Right(sendEmailAddr, Len(sendEmailAddr) - InStr(sendEmailAddr, "@"))

' ### USER OPTIONS ###
blnOverwrite = False ' False = don't overwrite, True = do overwrite

'### THIS IS WHERE SAVE LOCATIONS ARE SET ###
'Currently only saves to yPath. Change the yPath variable to mPath in other areas of the script to enable the month folder.
bPath = "C:\email\" 'Defines the base path to save the email
cPath = bPath & companyDomain & "\" 'Adds company domain to base path
yPath = cPath & Format(Now(), "yyyy") & "\" 'Add year subfolder
mPath = yPath & Format(Now(), "MMMM") & "\" 'Add month subfolder

'### Path Validity ###
'Make sure base path exists
If Dir(bPath, vbDirectory) = vbNullString Then
   MkDir bPath
End If
'Make sure company domain path exists
'If Dir(cPath, vbDirectory) = vbNullString Then
   'MkDir cPath
'End If
'Make sure year path exists
'If Dir(yPath, vbDirectory) = vbNullString Then
   'MkDir yPath
'End If
'Make sure month path exists (uncomment below lines to enable)
'If Dir(mPath, vbDirectory) = vbNullString Then
 'MkDir mPath
'End If

'### Get Email subject & set name to be saved as ###
emailSubject = CleanFileName(oMail.Subject)
saveName = Format(oMail.ReceivedTime, "yyyymmdd") & "_" & emailSubject & ".txt"
Set fso = CreateObject("Scripting.FileSystemObject")

'### If don't overwrite is on then ###
If blnOverwrite = False Then
   looper = 0
   Do While fso.FileExists(yPath & saveName)
      looper = looper + 1
      saveName = Format(oMail.ReceivedTime, "yyyymmdd") & "_" & emailSubject & "_" & looper & ".txt"
   Loop
Else '### If don't overwrite is off, delete the file ###
   If fso.FileExists(yPath & saveName) Then
      fso.DeleteFile yPath & saveName
   End If
End If

'### Save MSG File ###
oMail.SaveAs bPath & saveName, olTXT

'### If Mail Attachments: clean file name, save into path ###
If oMail.Attachments.Count > 0 Then
   For Each atmt In oMail.Attachments
      atmtName = CleanFileName(atmt.FileName)
      atmtSave = bPath & Format(oMail.ReceivedTime, "yyyymmdd") & "_" & atmtName
      atmt.SaveAsFile atmtSave
   Next
End If

Set oMail = Nothing
Set olNS = Nothing
Set fso = Nothing
End Sub

Function CleanFileName(strText As String) As String
Dim strStripChars As String
Dim intLen As Integer
Dim i As Integer
strStripChars = "/\[]:=," & Chr(34)
intLen = Len(strStripChars)
strText = Trim(strText)
For i = 1 To intLen
strText = Replace(strText, Mid(strStripChars, i, 1), "")
Next
CleanFileName = strText
End Function



Sub SaveAsPDF(MyMail As MailItem)
' requires reference to Microsoft Scripting Runtime
' \Windows\System32\Scrrun.dll
' Also requires reference to Microsoft Word Object Library
Dim fso As FileSystemObject
Dim strSubject As String
Dim strSaveName As String
Dim blnOverwrite As Boolean
Dim strFolderPath As String
Dim looper As Integer
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim oMail As Outlook.MailItem

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set oMail = olNS.GetItemFromID(strID)

'Get Sender email domain
sendEmailAddr = oMail.SenderEmailAddress
companyDomain = Right(sendEmailAddr, Len(sendEmailAddr) - InStr(sendEmailAddr, "@"))

' ### USER OPTIONS ###
blnOverwrite = False ' False = don't overwrite, True = do overwrite

'### THIS IS WHERE SAVE LOCATIONS ARE SET ###
bPath = "C:\email\" 'Defines the base path to save the email
cPath = bPath & companyDomain & "\" 'Adds company domain to base path
yPath = cPath & Format(Now(), "yyyy") & "\" 'Add year subfolder
mPath = yPath & Format(Now(), "MMMM") & "\" 'Add month subfolder

'### Path Validity ###
If Dir(bPath, vbDirectory) = vbNullString Then
    MkDir bPath
End If
'If Dir(cPath, vbDirectory) = vbNullString Then
   ' MkDir cPath
'End If
'If Dir(yPath, vbDirectory) = vbNullString Then
   ' MkDir yPath
'End If

'### Get Email subject & set name to be saved as ###
emailSubject = CleanFileName(oMail.Subject)
saveName = Format(oMail.ReceivedTime, "yyyy-mm-dd-hhmm") & "_" & emailSubject & ".mht"
Set fso = CreateObject("Scripting.FileSystemObject")

'### If don't overwrite is on then ###
If blnOverwrite = False Then
    looper = 0
    Do While fso.FileExists(bPath & saveName)
        looper = looper + 1
        saveName = Format(oMail.ReceivedTime, "yyyy-mm-dd-hhmm") & "_" & emailSubject & "_" & looper & ".mht"
        pdfSave = Format(oMail.ReceivedTime, "yyyy-mm-dd-hhmm") & "_" & emailSubject & "_" & looper & ".pdf"
        Loop
Else '### If don't overwrite is off, delete the file ###
    If fso.FileExists(bPath & saveName) Then
        fso.DeleteFile bPath & saveName
    End If
End If
oMail.SaveAs bPath & saveName, olMHTML
pdfSave = bPath & Format(oMail.ReceivedTime, "yyyy-mm-dd-hhmm") & "_" & emailSubject & ".pdf"

'### Open Word to convert file to PDF ###
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")

Set wrdDoc = wrdApp.Documents.Open(FileName:=bPath & saveName, Visible:=True)
wrdApp.ActiveDocument.ExportAsFixedFormat OutputFileName:= _
            pdfSave, ExportFormat:= _
            wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
            wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=0, To:=0, _
            Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
            CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=True, UseISO19005_1:=False

wrdDoc.Close
wrdApp.Quit

'### Clean up files ###
With New FileSystemObject
    If .FileExists(bPath & saveName) Then
        .DeleteFile bPath & saveName
    End If
End With

'### If Mail Attachments: clean file name, save into path ###
If oMail.Attachments.Count > 0 Then
    For Each atmt In oMail.Attachments
        atmtName = CleanFileName(atmt.FileName)
        atmtSave = bPath & Format(oMail.ReceivedTime, "yyyy-mm-dd-hhmm") & "_" & atmtName
        atmt.SaveAsFile atmtSave
    Next
End If

Set oMail = Nothing
Set olNS = Nothing
Set fso = Nothing
End Sub

如果有人有任何想法,将不胜感激。

【问题讨论】:

  • 您可以使用Dir函数来测试文件是否已经存在。如果它已经存在,那么你需要给它一个新的文件名。
  • 我是否必须创建唯一标识符,或者我是否可以循环遍历大约 10 个数字以添加到文件名的末尾?
  • 您为什么不尝试一些方法,看看哪些有效(或无效)?

标签: vba email outlook


【解决方案1】:

一旦您删除了删除文件的if 语句,这将非常有效。谢谢你的基金会。

我已经修改了您的代码的 PDF 部分(希望更好)并修复了一个问题,即如果 pdf 文件名已经存在,它不会增加。我不得不为 PDF 编写一个单独的循环,因为你基本上用这条线停止了循环:pdfSave = bPath & Format(oMail.ReceivedTime, "yyyy-mm-dd-hhmm") & "_" & emailSubject & ".pdf" 但我似乎无法摆脱那条线而不产生错误,所以创建了一个新循环。也许有人可以为我简化这部分。

我还添加了一行删除仅用于创建 PDF 的 .mht 文件并稍微修改了文件名:

Function CleanFileName(strText As String) As String
Dim strStripChars As String
Dim intLen As Integer
Dim i As Integer
strStripChars = "/\[]:=," & Chr(34)
intLen = Len(strStripChars)
strText = Trim(strText)
For i = 1 To intLen
strText = Replace(strText, Mid(strStripChars, i, 1), "")
Next
CleanFileName = strText
End Function



Sub SaveAsPDF(MyMail As MailItem)
' ### Requires reference to Microsoft Scripting Runtime ###
' ### Requires reference to Microsoft Word Object Library ###
' --- In VBE click TOOLS > REFERENCES and check the boxes for both of the above ---
Dim fso As FileSystemObject
Dim strSubject As String
Dim strSaveName As String
Dim blnOverwrite As Boolean
Dim strFolderPath As String
Dim sendEmailAddr As String
Dim senderName As String
Dim looper As Integer
Dim plooper As Integer
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim oMail As Outlook.MailItem

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set oMail = olNS.GetItemFromID(strID)

' ### Get username portion of sender email address ###
sendEmailAddr = oMail.SenderEmailAddress
senderName = Left(sendEmailAddr, InStr(sendEmailAddr, "@") - 1)

' ### USER OPTIONS ###
blnOverwrite = False ' False = don't overwrite, True = do overwrite

' ### Path to save directory ###
bPath = "Z:\email\"

' ### Create Directory if it doesnt exist ###
If Dir(bPath, vbDirectory) = vbNullString Then
    MkDir bPath
End If

' ### Get Email subject & set name to be saved as ###
emailSubject = CleanFileName(oMail.Subject)
saveName = Format(oMail.ReceivedTime, "yyyy-mm-dd-hhmm") & "_" & emailSubject & ".mht"
Set fso = CreateObject("Scripting.FileSystemObject")

' ### Increment filename if it already exists ###
If blnOverwrite = False Then
    looper = 0
    Do While fso.FileExists(bPath & saveName)
        looper = looper + 1
        saveName = Format(oMail.ReceivedTime, "yyyy-mm-dd-hhmm") & "_" & senderName & "_" & emailSubject & "_" & looper & ".mht"
        Loop
Else
End If

' ### Save .mht file to create pdf from Word ###
oMail.SaveAs bPath & saveName, olMHTML
pdfSave = bPath & Format(oMail.ReceivedTime, "yyyy-mm-dd-hhmm") & "_" & senderName & "_" & emailSubject & ".pdf"

If fso.FileExists(pdfSave) Then
    plooper = 0
    Do While fso.FileExists(pdfSave)
    plooper = plooper + 1
    pdfSave = bPath & Format(oMail.ReceivedTime, "yyyy-mm-dd-hhmm") & "_" & senderName & "_" & emailSubject & "_" & plooper & ".pdf"
    Loop
Else
End If


' ### Open Word to convert .mht file to PDF ###
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")

' ### Open .mht file we just saved and export as PDF ###
Set wrdDoc = wrdApp.Documents.Open(FileName:=bPath & saveName, Visible:=True)
wrdApp.ActiveDocument.ExportAsFixedFormat OutputFileName:= _
            pdfSave, ExportFormat:= _
            wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
            wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=0, To:=0, _
            Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
            CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=True, UseISO19005_1:=False

wrdDoc.Close
wrdApp.Quit

' ### Delete .mht file ###
Kill bPath & saveName

' ### Uncomment this section to save attachments ###
'If oMail.Attachments.Count > 0 Then
'    For Each atmt In oMail.Attachments
'        atmtName = CleanFileName(atmt.FileName)
'        atmtSave = bPath & Format(oMail.ReceivedTime, "yyyy-mm-dd-hhmm") & "_" & atmtName
'        atmt.SaveAsFile atmtSave
'    Next
'End If

Set oMail = Nothing
Set olNS = Nothing
Set fso = Nothing
End Sub

【讨论】:

    【解决方案2】:

    我注意到以下代码行:

    strID = MyMail.EntryID
    Set olNS = Application.GetNamespace("MAPI")
    Set oMail = olNS.GetItemFromID(strID)
    

    无需获取 MailItem 类的新实例。您可以使用作为参数传递的实例。

     If fso.FileExists(bPath & saveName) Then
        fso.DeleteFile bPath & saveName
    

    您似乎删除了现有文件,而不是保存具有不同名称的新文件。

    您可以考虑在保存电子邮件/附件时使用日期时间(不仅仅是日期)标记。或者您可以检查磁盘上是否已经存在此类文件。

    【讨论】:

    • 感谢您的帮助! oMail.RecievedTime 和 datetime 有什么区别?我已经将文件保存到第二个,但是当它们一次发送时,有时文件没有保存。
    • 我删除了删除文件的代码,但我不明白你答案的第一部分(我对 vba 有点陌生)。其中哪个是 MailItem 类的新实例,应该删除其中的一些吗?请在下面查看我的答案,让我知道如何改进我所拥有的。
    猜你喜欢
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 2023-03-22
    • 2016-06-14
    • 2021-06-26
    • 1970-01-01
    • 2019-04-24
    相关资源
    最近更新 更多