gpt4 book ai didi

vba - 在本地目录中创建文件夹后保存 Outlook 附件

转载 作者:行者123 更新时间:2023-12-02 17:58:15 29 4
gpt4 key购买 nike

我正在尝试通过收件箱中的名称“MacroEnabled”访问子文件夹,找到其中的所有附件并将它们保存到本地驱动器。

我使用此代码创建一个名为“Documents”的文件夹并保存附件。然而,在进行第二次迭代时,它说文件已存在错误“58”。

    Dim ns As NameSpace
Dim olFolder_Inbox As Folder
Dim olMail As MailItem
Dim olAttachment As Attachment
Dim FolderPath As String
Dim fso As Object
Dim File_Saved As String

'email service type
Set ns = GetNamespace("MAPI")
Set olFolder_Inbox = ns.GetDefaultFolder(olFolderInbox).Folders("MacroEnabled")

Set fso = CreateObject("Scripting.FileSystemObject")
FolderPath = "Documents"
For Each olMail In olFolder_Inbox.Items

If TypeName(olMail) = "MailItem" And olMail.Attachments.Count > 0 Then
fso.CreateFolder ("Documents")

For Each olAttachment In olMail.Attachments

olAttachment.SaveAsFile fso.BuildPath(FolderPath, olAttachment.FileName)

Next olAttachment

End If

Next olMail

Set ns = Nothing
Set fso = Nothing

End Sub

最佳答案

首先 FolderPath 应显示整个路径,例如。 FolderPath = "C:\Documents"如果需要,可以使用相对路径,例如 FolderPath = CurrentProject.Path & "\Documents"然后您可以通过添加以下指令在循环中使用FolderExisits方法:

If Not fso.FolderExists(FolderPath) Then fso.CreateFolder (FolderPath )

关于vba - 在本地目录中创建文件夹后保存 Outlook 附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56306034/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com