Excel VBA,如何回复特定的电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28612018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA, how to Reply to a specific email message
提问by aurezio
I receive a mail every wednesday from a specific sender. The subject of this email sometimes changes
我每个星期三都会收到来自特定发件人的邮件。这封电子邮件的主题有时会改变
Example #1 of subject "Exposure statement - COB 20150217"
主题“暴露声明 - COB 20150217”的示例 #1
Example #2 of subject "Margin Notice COB 2015-Feb-10"
主题“保证金通知 COB 2015-Feb-10”的示例 #2
The date the sender append is the day before the day I receive the mail.
发件人附加的日期是我收到邮件的前一天。
I have the following code wich might search for that email and then reply to it with a custom body text but I can't manage to let the code to find that specific message with that date in the subject.
我有以下代码,它可能会搜索该电子邮件,然后使用自定义正文文本回复它,但我无法让代码找到主题中带有该日期的特定消息。
Is there a way to search by other parameters than the subject?
有没有办法通过主题以外的其他参数进行搜索?
Sub ReplyMail_No_Movements()
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Variant
Dim SigString As String
Dim Signature As String
Dim i As Integer
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
i = 1
SigString = Environ("appdata") & _
"\Microsoft\Signatures\MCC.txt"
If Dir(SigString) <> "" Then
Signature = GetBoiler(SigString)
Else
Signature = ""
End If
On Error Resume Next
For Each olMail In Fldr.Items
If InStr(olMail.Subject, "Exposure Statement - COB date") <> 0 Then 'where date is a date that changes every wednesday
With olMail.Reply
.to = "[email protected];[email protected]"
.CC = "[email protected];[email protected]"
.Body = "Dear All," & Chr(10) & _
Chr(10) & "we agree with your portfolio here attached and according to it we see no move for today." & _
Chr(10) & " Best Regards." & _
Chr(10) & _
Chr(10) & Signature
.Display
End With
i = i + 1
End If
Next olMail
End Sub
Edit: I changed this code bit from
编辑:我改变了这个代码位
If InStr(olMail.Subject, "Exposure Statement - COB date") <> 0 Then
to
到
If olMail.SenderEmailAddress = "[email protected]" And olMail.ReceivedTime = Now() Then
But it doesn't work...
但它不起作用...
This is the only search combo (SenderEmailAddressthat and ReceivedTime) that let me find the exact message...
这是唯一能让我找到确切消息的搜索组合(SenderEmailAddressthat 和 ReceivedTime)...
采纳答案by BrakNicku
You shoud use: Tools->References. Find Microsoft Outlook 15.0 Object Library
, check it and close the window.
您应该使用:工具-> 参考。查找Microsoft Outlook 15.0 Object Library
,检查并关闭窗口。
回答by brettdj
Or just use late binding
或者只是使用后期绑定
Const olFolderInbox = 6
Sub Test()
Dim olApp As Object
Dim olNs As Object
Dim Fldr As Object
Dim olMail
Dim i As Long
Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
i = 1
For Each olMail In Fldr.Items
If InStr(olMail.Subject, "email message object text") <> 0 Then
olMail.Display
olMail.ReplyAll
i = i + 1
End If
Next olMail
End Sub