Скачать приложение Outlook с темой - PullRequest
0 голосов
/ 29 апреля 2020

Я получил этот код 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

1 Ответ

1 голос
/ 29 апреля 2020

Попробуйте

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

Вам нужны кавычки вокруг строковых значений

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...