vba - 将附件保存到新的 Windows 文件夹?
问题描述
每次收到主题为“测试”的电子邮件时,我都想:
- 自动提取所有附件并将它们存储在自己新创建的文件夹中。
- 自动复制此新文件夹中的电子邮件
- 在这个新文件夹中自动添加一个 Word 文档。
- 该文件夹必须以收到日期命名。
我拥有的代码复制了预选文件夹中的所有附件,但它不会为它们创建个人文件夹。
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub Items_ItemAdd(ByVal item As Object)
On Error GoTo ErrorHandler
'Only act if it's a MailItem
Dim Msg As Outlook.MailItem
If TypeName(item) = "MailItem" Then
Set Msg = item
'Change variables to match need. Comment or delete any part unnecessary.
If (Msg.Subject = "Heures") And _
(Msg.Attachments.Count >= 1) Then
'Set folder to save in.
Dim olDestFldr As Outlook.MAPIFolder
Dim myAttachments As Outlook.Attachments
Dim Att As Variant
Const attPath As String = "C:\Users\NASC02\Test\"
' save attachment
Set myAttachments = item.Attachments
For Each Att In myAttachments
Att.SaveAsFile attPath & Att.FileName
Next
' mark as read
Msg.UnRead = False
End If
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
解决方案
编码
Set myAttachments = item.Attachments
Att = myAttachments.item(1).DisplayName
myAttachments.item(1).SaveAsFile attPath & Att
需要改为
Set myAttachments = item.Attachments
for each Att in myAttachments
Att.SaveAsFile attPath & Att.FileName
next
推荐阅读
- r - ggplot:当它们接近特定值时按密度着色点?
- html - 操作复选框以在未选中时让 CSS 旋转
- node.js - Google Sheets API NodeJS - 在不更改格式的情况下更新单元格
- .htaccess - 将这两种条件格式组合在一个 htaccess 中
- node.js - 返回具有相同日期的对象集合(MongoDB/Mongoose)
- python - 如何在 matplotlib 中标记空间刻度线并自定义网格线
- java - Spring Hibernate - 使用属性转换器将实体列表保留为 ID 数组
- python - 等效于 pybind11 中的 boost::python py::scope().attr()
- c++ - 从 C++ 问题中的函数返回数组地址
- php - php fputcsv 保存为二进制文件