excel - 从 Outlook 收件箱下载 Excel 相关附件并使用 VBA 根据日期范围将其保存在特定文件夹中?
问题描述
在这里,我想根据日期范围从 Outlook 应用程序下载所有与 Excel 相关的附件。
所有下载的附件都将其保存在桌面文件夹中。
当我运行以下代码时,它会从 Outlook 下载所有附件并保存在 mydocuments 文件夹中。
Const sPath As String = "C:\Users\Documents\Attachments\" Sub Shortage_Attachments3() Dim ns As Namespace Dim Inbox As MAPIFolder, SubFolder As MAPIFolder Dim Item As Object Dim Atmt As Attachment Dim FileName As String Set ns = GetNamespace("MAPI") Set Inbox = ns.GetDefaultFolder(olFolderInbox) On Error Resume Next For Each Item In Inbox.Items For Each Atmt In Item.Attachments FileName = Atmt.FileName If Len(dir(sPath & FileName)) > 0 Then FileName = sPath & Format(Item(I).ReceivedTime, "DDMMYYYY") & "_" & Format(Now, "DDMMYYHHMMSS") & FileName Atmt.SaveAsFile FileName Next Atmt Next Item MsgBox "Download Complete.", vbInformation, "SUCCESS" End Sub
解决方案
我已经修改了您的代码以获取附件中文件的扩展名,然后检查它是否是 xl 文件。您能否尝试运行代码,如果您有任何疑问,请告诉我。谢谢。
Const sPath As String = "C:\Users\Documents\Attachments\"
Sub Shortage_Attachments3()
Dim ns As namespace
Dim Inbox As MAPIFolder, SubFolder As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim blnxlFile As Boolean
Dim intPos As Integer
Dim strExtn As String
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
On Error Resume Next
For Each Item In Inbox.Items
For Each Atmt In Item.Attachments
blnxlFile = False
FileName = Atmt.FileName
intPos = InStr(1, FileName, ".", vbTextCompare)
If intPos > 0 Then
strExtn = Mid(FileName, intPos + 1, Len(FileName) - intPos)
If Left(strExtn, 2) = "xl" Then
blnxlFile = True
End If
End If
If Len(Dir(sPath & FileName)) > 0 And blnxlFile = True Then
FileName = sPath & Format(Item(i).ReceivedTime, "DDMMYYYY") & "_" & Format(Now, "DDMMYYHHMMSS") & FileName
Atmt.SaveAsFile FileName
End If
Next Atmt
Next Item
MsgBox "Download Complete.", vbInformation, "SUCCESS"
End Sub
推荐阅读
- reactjs - 在 ReactJS 中使用客户端证书
- swift - 使用 Swift PDFKit 的 PDF 大小膨胀
- swift - SwiftUI 字典 - 编译器无法在合理的时间内对该表达式进行类型检查
- c - 尝试制作一个简单的程序(在 C 中),将给定文本文件中的所有非空行复制到新的文本文件中
- json - Django - 访问 Json
- html - 如何防止谷歌翻译,更改日期格式?
- data-structures - 在堆数据结构中,你可以有一个100的父节点和100和0的2个子节点,它有什么实际用途吗?
- iis - IIS 日志记录 - cs-uri-stem 与已知内容不匹配
- asp.net-core - 如何通过接口获取当前登录的用户名以在asp.net core mvc中按列更新?
- python - 在 Ubuntu 20.04 上删除 Python 3 的手动安装