重命名和删除规则中代码的附件时出现问题

时间:2018-08-29 17:07:34

标签: vba outlook

我有这段代码,它可以使附件脱离Outlook并进入本地文件夹。此后,我一直试图对其进行修改以重命名该文件,并从Outlook中删除该电子邮件,这就是它停止工作的地方。

该规则将电子邮件进入新文件夹时移动,然后将附件保存到C驱动器上的文件夹中。每天只有1封电子邮件,每天只有1个附件。

我想将附件保存到文件夹,重命名附件(覆盖现有文件),然后从Outlook中删除电子邮件。

这是我到目前为止的代码。

任何帮助将不胜感激

Public Sub SaveAttachments(Item As Outlook.MailItem)

If Item.Attachments.Count > 0 Then

Dim objAttachments As Outlook.Attachments
Dim lngCount As Long
Dim strFile As String
Dim sFileType As String
Dim i As Long

Set objAttachments = Item.Attachments
lngCount = objAttachments.Count
For i = lngCount To 1 Step -1

' Get the file name.
strFile = objAttachments.Item(i).FileName

' Get the path to your My Documents folder
strfolderpath = "C:\Automation\CBM\"
'strfolderpath = strfolderpath & "\Attachments\"

' Combine with the path to the folder.
strFile = strfolderpath & strFile

' Save the attachment as a file.
objAttachments.Item(i).SaveAsFile FilePath & "Daily_Activity_Report" & 
".xlsx"

' Delete the attachment.
objAttachments.Item(i).Delete

Next i
End If

End Sub

1 个答案:

答案 0 :(得分:0)

尝试一下:

Public Sub SaveAttachments(Item As Outlook.MailItem)

Dim objAttachments As Outlook.Attachments
Dim lngCount As Long
Dim i As Long

If Item.Attachments.Count > 0 Then

    Set objAttachments = Item.Attachments

    lngCount = objAttachments.Count

    For i = lngCount To 1 Step -1

        'Save the attachment as a file.
        objAttachments.Item(i).SaveAsFile "C:\Automation\CBM\Daily_Activity_Report.xlsx"

        'Delete the attachment.
        objAttachments.Item(i).Delete

    Next i

    Item.Save

End If

End Sub