【问题标题】:VBA/Outlook extracting attachments from .eml filesVBA/Outlook 从 .eml 文件中提取附件
【发布时间】:2020-06-24 18:04:58
【问题描述】:

我正在尝试获取一个包含 .eml 邮件和附件的文件夹,然后将附件提取/重命名/保存到另一个文件夹中。我的代码:

Sub SaveAttachments()
    Dim OlApp As Outlook.Application
    Set OlApp = GetObject(, "Outlook.Application")
    Dim MsgFilePath
    Dim Eml As Outlook.MailItem
    Dim att As Outlook.Attachments
    Dim Path As String
    Path = "C:\Users\richard\Desktop\Inbox\"

    If OlApp Is Nothing Then
        Err.Raise ERR_OUTLOOK_NOT_OPEN
    End If

    Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    Dim temp As Object
    Set temp = fs.GetFolder(Path)

    For Each MsgFilePath In temp.Files
        Set Eml = OlApp.CreateItemFromTemplate(Path & MsgFilePath.Name)

    Set att = Eml.Attachments
        If att.Count > 0 Then
            For i = 1 To att.Count
                fn = "C:\Users\richard\Desktop\cmds\" & Eml.SenderEmailAddress
                att(i).SaveAsFile fn
            Next i
        End If


        Set Eml = Nothing
    Next

    Set OlApp = Nothing
End Sub

但是我在循环中的第一个文件上直接得到了这个错误,即行 设置 Eml = OlApp.CreateItemFromTemplate(Path & MsgFilePath.Name) :

-2147286960 (80030050)    %1 already exists. 

非常感谢您对正在发生的事情的任何想法!

【问题讨论】:

  • 鸟瞰:既然你没有提到哪一行,那是att(i).SaveAsFile fn这一行吗?
  • 此外,如果有多个来自同一发件人的电子邮件,那么您的代码将尝试覆盖文件..."C:\Users\richard\Desktop\cmds\" & Eml.SenderEmailAddress
  • 感谢您的建议 - 我可以确认错误发生在 FIRST 循环中(因此尚未打开/创建其他文件),并且以防万一所有电子邮件的地址都不同。已更新问题以显示导致错误的行
  • 片刻测试一下
  • 整个错误消息是运行时错误“-2147286960 (80030050)”:无法打开文件:C:\Users\Mauro\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content。 ....该文件可能不存在,您可能没有打开它的权限,或者它可能在另一个程序中打开。右键单击包含该文件的文件夹,然后单击“属性”以检查您对该文件夹的权限。

标签: vba outlook


【解决方案1】:

试试这个(尝试和测试)

Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As _
String, ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWMINIMIZED As Long = 2

Sub SaveAttachments()
    Dim OlApp As Outlook.Application
    Set OlApp = GetObject(, "Outlook.Application")
    Dim MsgFilePath
    Dim Eml As Outlook.MailItem
    Dim att As Outlook.Attachments
    Dim sPath As String
    sPath = "C:\Users\richard\Desktop\Inbox\"

    If OlApp Is Nothing Then
        Err.Raise ERR_OUTLOOK_NOT_OPEN
    End If

    sFile = Dir(sPath & "*.eml")

    Do Until sFile = ""
        ShellExecute 0, "Open", sPath & sFile, "", sPath & sFile, SW_SHOWNORMAL

        Wait 2

        Set MyInspect = OlApp.ActiveInspector
        Set Eml = MyInspect.CurrentItem

        Set att = Eml.Attachments
        If att.Count > 0 Then
            For i = 1 To att.Count
                fn = "C:\Users\richard\Desktop\cmds\" & i & "-" & Eml.SenderEmailAddress
                att(i).SaveAsFile fn
            Next i
        End If

        sFile = Dir$()
    Loop

    Set OlApp = Nothing
End Sub

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub

【讨论】:

  • 感谢您,但仍然遇到同样的错误。我只是注意到文件夹中的消息是 .eml 格式的 - 也许这是问题的根源?
  • sFile = Dir(sPath & "*.msg")改为sFile = Dir(sPath & "*.*"),然后在Do Until sFile = ""之后插入这一行Msgbox sFile你在msgbox中看到了什么?
  • 我得到文件夹中第一个文件的名称,加上扩展名,例如。 foob​​ar.eml
  • 按下确定按钮后会发生什么?你得到错误了吗?
  • 使用 Outlook 2010, vba 7.0
猜你喜欢
  • 1970-01-01
  • 2018-06-25
  • 2021-07-21
  • 2020-03-20
  • 1970-01-01
  • 2011-11-12
  • 1970-01-01
  • 2022-12-09
  • 1970-01-01
相关资源
最近更新 更多