下载主题为的outlook附件



我得到了这段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

尝试

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

字符串值周围需要引号

最新更新