Excel 下载带有主题的outlook附件
我得到了这个VBA代码,并对其进行了编辑,以便按主题从电子邮件中下载附件,但它不识别任何主题 我想用Excel运行它 有人能指出错误在哪里吗Excel 下载带有主题的outlook附件,excel,vba,outlook,Excel,Vba,Outlook,我得到了这个VBA代码,并对其进行了编辑,以便按主题从电子邮件中下载附件,但它不识别任何主题 我想用Excel运行它 有人能指出错误在哪里吗 Const olFolderInbox As Integer = 6 '~~> Path for the attachment Const AttachmentPath As String = "C:\" Sub DownloadAttachmentFirstUnreadEmail() Dim oOlAp As Object, oOlns
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
Const olFolderInbox作为整数=6
“~~>附件的路径
Const AttachmentPath As String=“C:\”
子下载附件FirstUnreadeMail()
将oOlAp作为对象、oOlns作为对象、oOlInb作为对象
将oOlItm设置为对象,将oOlAtch设置为对象
“~~>附件的新文件名
将NewFileName设置为字符串
NewFileName=AttachmentPath
“~~>获取Outlook实例
设置oOlAp=GetObject(,“Outlook.application”)
设置oOlns=oOlAp.GetNamespace(“MAPI”)
设置oOlInb=oOlns.GetDefaultFolder(olFolderInbox)
“~~>检查是否有任何实际未读的电子邮件
如果oOlInb.Items.Restrict(“[Subject]=”和“Sample Subject”).Count=0,则
MsgBox“收件箱中没有[Subject]=示例主题的电子邮件”
如果结束
“~~>从第一封未读电子邮件中提取附件
对于oOlInb.Items.Restrict中的每个oOlItm(“[主题]=”和“示例主题”)
“~~>检查电子邮件是否有附件
如果oOlItm.Attachments.Count为0,则
对于oOlItm.附件中的每个oOlAtch
“~~>下载附件
oOlAtch.SaveAsFile NewFileName&oOlAtch.Filename
退出
下一个
其他的
MsgBox“第一项没有附件”
如果结束
退出
下一个
端接头
试试看
您需要在字符串值周围加引号才能获得包含部分文本的主题,我应该使用“Sample subject*”?有关使用通配符的信息,请参见此处-
oOlInb.Items.Restrict("[Subject] ='" & "Sample Subject" & "'")