【问题标题】:Download outlook attachment with subject下载带有主题的 Outlook 附件
【发布时间】:2020-04-29 09:43:18
【问题描述】:

我得到了这个 VBA 代码并对其进行了编辑,以便按主题从电子邮件中下载附件,但它无法识别任何主题。

我想用 Excel 运行它

谁能指出错误在哪里?

Const olFolderInbox As Integer = 6
'~~> Path for the attachment
Const AttachmentPath As String = "C:\"

Sub DownloadAttachmentFirstUnreadEmail()
    Dim oOlAp As Object, oOlns As Object, oOlInb As Object
    Dim oOlItm As Object, oOlAtch As Object

    '~~> New File Name for the attachment
    Dim NewFileName As String
    NewFileName = AttachmentPath

    '~~> Get Outlook instance
    Set oOlAp = GetObject(, "Outlook.application")
    Set oOlns = oOlAp.GetNamespace("MAPI")
    Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)

    '~~> Check if there are any actual unread emails
    If oOlInb.Items.Restrict("[Subject] =" & "Sample Subject").Count = 0 Then
        MsgBox "NO Email In Inbox with [Subject] = Sample Subject"
    End If

    '~~> Extract the attachment from the 1st unread email
    For Each oOlItm In oOlInb.Items.Restrict("[Subject] =" & "Sample Subject")
        '~~> Check if the email actually has an attachment
        If oOlItm.Attachments.Count <> 0 Then
            For Each oOlAtch In oOlItm.Attachments
                '~~> Download the attachment
                oOlAtch.SaveAsFile NewFileName & oOlAtch.Filename
                Exit For
            Next
        Else
            MsgBox "The First item doesn't have an attachment"
        End If
        Exit For
    Next
 End Sub

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:

    试试

    oOlInb.Items.Restrict("[Subject] ='" & "Sample Subject" & "'") 
    

    字符串值需要引号

    【讨论】:

    猜你喜欢
    • 2017-12-18
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多