从 Outlook 电子邮件中提取消息附件

发布于 2024-11-17 15:48:38 字数 629 浏览 3 评论 0原文

我有以下 vba 代码,用于将附件保存在电子邮件中。

这对于 .docx、.jpg 等文件效果很好,但我需要用它来提取多个 .msg 附件,但这是行不通的。

代码是

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String

saveFolder = "C:\Test\"
For Each objAtt In itm.Attachments
stFileName = saveFolder & "\" & objAtt.DisplayName
i = 0
JumpHere:
If Dir(stFileName) = "" Then
objAtt.SaveAsFile stFileName
Else
i = i + 1
stFileName = saveFolder & "\" & i & " - " & objAtt.DisplayName
GoTo JumpHere
End If
Set objAtt = Nothing
Next
End Sub

错误涉及行 - If Dir(stFileName) = "" Then

I have the following vba code that saves attachments in an email.

This works fine for .docx, .jpg etc but I need to use it to extract multiple .msg attachments which doesn't work.

The code is

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String

saveFolder = "C:\Test\"
For Each objAtt In itm.Attachments
stFileName = saveFolder & "\" & objAtt.DisplayName
i = 0
JumpHere:
If Dir(stFileName) = "" Then
objAtt.SaveAsFile stFileName
Else
i = i + 1
stFileName = saveFolder & "\" & i & " - " & objAtt.DisplayName
GoTo JumpHere
End If
Set objAtt = Nothing
Next
End Sub

The error concerns the line - If Dir(stFileName) = "" Then

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

银河中√捞星星 2024-11-24 15:48:38

在我们的聊天之后,这是最终的代码:

Public Sub saveAttachtoDisk(itm As Outlook.MailItem) 
Dim objAtt As Outlook.Attachment 
Dim saveFolder As String 
Dim i As Integer 

saveFolder = "C:\Test\" 
For Each objAtt In itm.Attachments 
  stFileName = saveFolder & objAtt.FileName 
  i = 0 
  'Loop to find the first available filename 
  Do While Dir(stFileName) <> "" 
    i = i + 1 
    stFileName = saveFolder & i & " - " & objAtt.FileName 
  Loop
  objAtt.SaveAsFile stFileName 
Next 
End Sub

问候,

Max

Following our chat, here is the final code :

Public Sub saveAttachtoDisk(itm As Outlook.MailItem) 
Dim objAtt As Outlook.Attachment 
Dim saveFolder As String 
Dim i As Integer 

saveFolder = "C:\Test\" 
For Each objAtt In itm.Attachments 
  stFileName = saveFolder & objAtt.FileName 
  i = 0 
  'Loop to find the first available filename 
  Do While Dir(stFileName) <> "" 
    i = i + 1 
    stFileName = saveFolder & i & " - " & objAtt.FileName 
  Loop
  objAtt.SaveAsFile stFileName 
Next 
End Sub

Regards,

Max

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文