【问题标题】:Trouble renaming and deleting attachment from code in a rule无法从规则中的代码重命名和删除附件
【发布时间】:2018-08-31 01:04:17
【问题描述】:

我有这段代码,它可以将附件从 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

【问题讨论】:

  • 什么是FilePath
  • 标题“重命名和删除附件”与正文“重命名文件并删除电子邮件”不匹配。哪个是正确的?

标签: vba outlook


【解决方案1】:

试试这个:

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

【讨论】:

  • 一切正常,但删除。我想知道这是否是因为第一步是将其从收件箱移动到 CBM 子文件夹,并且如果删除是在邮件不再存在的收件箱中查找。不知道如何指定文件夹名称...基本上一旦它运行的脚本转到 CBM 文件夹并删除其中的所有内容
  • @Mindbender 我已经在里面添加了Item.Save,看来你必须在没有附件的情况下保存它。
  • 它似乎没有覆盖文件夹中的文件。有没有办法强制它覆盖它?
  • 我想出了一个解决方法,每天晚上使用任务计划程序存档和删除文件,以便可以在文件夹中创建新文件。将在接下来的几天内进行测试...感谢所有帮助
猜你喜欢
  • 2018-10-11
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多