检查是否有未读的电子邮件,附件名称包含;生产计划;作为名称的一部分,使用excel-vba
我正在使用excel vba和outlook进行一个项目 我在excel工作簿中工作。我需要能够运行宏以便:检查是否有未读的电子邮件,附件名称包含;生产计划;作为名称的一部分,使用excel-vba,vba,excel,outlook,Vba,Excel,Outlook,我正在使用excel vba和outlook进行一个项目 我在excel工作簿中工作。我需要能够运行宏以便: 检查是否有未读的电子邮件 Dim oOutlook As Object Dim oOlns As Object Dim oOlInb As Object Const olFolderInbox = 6 '~~> Get Outlook instance Set oOutlook = GetObject(, "Outlook.application") Set oOlns = o
Dim oOutlook As Object
Dim oOlns As Object
Dim oOlInb As Object
Const olFolderInbox = 6
'~~> Get Outlook instance
Set oOutlook = GetObject(, "Outlook.application")
Set oOlns = oOutlook.GetNamespace("MAPI")
Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)
'~~> Check if there are any actual unread emails
If oOlInb.Items.Restrict("[UnRead] = True").Count = 0 Then
MsgBox "NO Unread Email In Inbox"
Else
MsgBox "Unread Email available In Inbox"
Exit Sub
如果有未读的电子邮件更新:我做了一个小的补充,这不起作用。这是为了显示消息,如果检测到附件,但它们不是“生产计划”形式
我没有Outlook,所以未经测试: 编辑-列出所有附件
Dim oOutlook As Object
Dim oOlns As Object
Dim oOlInb As Object
Dim unRead, m As Object, att As Object
Dim some As String, other As String
Const olFolderInbox = 6
'~~> Get Outlook instance
Set oOutlook = GetObject(, "Outlook.application")
Set oOlns = oOutlook.GetNamespace("MAPI")
Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)
'~~> Check if there are any actual unread emails
Set unRead = oOlInb.Items.Restrict("[UnRead] = True")
If unRead.Count = 0 Then
MsgBox "NO Unread Email In Inbox"
Else
some = ""
other = ""
For Each m In unRead
If m.Attachments.Count > 0 Then
For Each att In m.Attachments
If att.Filename Like "Production Plan*.xls" Then
some = some & vbLf & " - " & att.Filename
Else
other = other & vbLf & " - " & att.Filename
End If
Next att
End If
Next m
If some <> "" Or other <> "" Then
MsgBox "Production Plans:" & vbLf & _
IIf(some <> "", some, "{none}") & _
vbLf & vbLf & "Other files:" & vbLf & _
IIf(other <> "", other, "{none}"), _
vbExclamation, "Unread mails with attachments!"
End If
End If
Dim oOutlook As Object
把乌龙当作对象
作为对象的Dim oOlInb
暗未读,m为对象,att为对象
调暗一些作为字符串,另一些作为字符串
常数olFolderInbox=6
“~~>获取Outlook实例
设置oOutlook=GetObject(,“Outlook.application”)
设置oOlns=oOutlook.GetNamespace(“MAPI”)
设置oOlInb=oOlns.GetDefaultFolder(olFolderInbox)
“~~>检查是否有任何实际未读的电子邮件
Set unRead=oOlInb.Items.Restrict(“[unRead]=True”)
如果未读。计数=0,则
MsgBox“收件箱中无未读电子邮件”
其他的
some=“”
其他=“”
对于未读的每一个m
如果m.Attachments.Count>0,则
对于m.附件中的每个附件
如果附件文件名类似于“生产计划*.xls”,则
some=some&vbLf&“-”和附件文件名
其他的
其他=其他&vbLf&“-”和附件文件名
如果结束
下一个att
如果结束
下一个m
如果是某个“”或其他“”,则
MsgBox“生产计划:”&vbLf&_
IIf(一些“”,一些“{none}”)&_
vbLf&vbLf&“其他文件:”&vbLf&_
IIf(其他“,”其他“{none}”)_
VBE感叹,“未读邮件及附件!”
如果结束
如果结束
您可能会发现Siddharth Rout给出的这个庞大的答案很有用:非常感谢。你的回答非常有用。它工作得很好。我做了一个小的添加,但不起作用。这是为了显示消息,如果检测到附件,但它们现在不是“生产计划”的形式,它将退出并在找到“正确”附件后立即停止检查。如果发现“错误”类型,该怎么办?您可能希望继续检查其他附件,而不是立即退出循环?
Dim oOutlook As Object
Dim oOlns As Object
Dim oOlInb As Object
Dim unRead, m As Object, att As Object
Dim some As String, other As String
Const olFolderInbox = 6
'~~> Get Outlook instance
Set oOutlook = GetObject(, "Outlook.application")
Set oOlns = oOutlook.GetNamespace("MAPI")
Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)
'~~> Check if there are any actual unread emails
Set unRead = oOlInb.Items.Restrict("[UnRead] = True")
If unRead.Count = 0 Then
MsgBox "NO Unread Email In Inbox"
Else
some = ""
other = ""
For Each m In unRead
If m.Attachments.Count > 0 Then
For Each att In m.Attachments
If att.Filename Like "Production Plan*.xls" Then
some = some & vbLf & " - " & att.Filename
Else
other = other & vbLf & " - " & att.Filename
End If
Next att
End If
Next m
If some <> "" Or other <> "" Then
MsgBox "Production Plans:" & vbLf & _
IIf(some <> "", some, "{none}") & _
vbLf & vbLf & "Other files:" & vbLf & _
IIf(other <> "", other, "{none}"), _
vbExclamation, "Unread mails with attachments!"
End If
End If