Excel VBA,如何回复特定的电子邮件
Excel VBA, how to Reply to a specific email message
我每个星期三都会收到一封来自特定发件人的邮件。这封电子邮件的主题有时会改变
主题示例 #1 "Exposure statement - COB 20150217"
主题示例 #2 "Margin Notice COB 2015-Feb-10"
发件人附加的日期是我收到邮件的前一天。
我有以下代码可以搜索该电子邮件,然后使用自定义正文回复它,但我无法让代码找到主题中包含该日期的特定邮件。
是否可以通过主题以外的其他参数进行搜索?
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 = "email1@domain.com;email2@domain.com"
.CC = "email3@domain.com;email4@domain.com"
.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
编辑:
我从
更改了此代码位
If InStr(olMail.Subject, "Exposure Statement - COB date") <> 0 Then
到
If olMail.SenderEmailAddress = "email1@gdomain.com" And olMail.ReceivedTime = Now() Then
但是不行...
这是唯一能让我找到准确邮件的搜索组合(SenderEmailAddressthat 和 ReceivedTime)...
您应该使用:工具->参考。找到 Microsoft Outlook 15.0 Object Library
,检查它并关闭 window。
或者只使用后期绑定
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
我每个星期三都会收到一封来自特定发件人的邮件。这封电子邮件的主题有时会改变
主题示例 #1 "Exposure statement - COB 20150217"
主题示例 #2 "Margin Notice COB 2015-Feb-10"
发件人附加的日期是我收到邮件的前一天。
我有以下代码可以搜索该电子邮件,然后使用自定义正文回复它,但我无法让代码找到主题中包含该日期的特定邮件。
是否可以通过主题以外的其他参数进行搜索?
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 = "email1@domain.com;email2@domain.com"
.CC = "email3@domain.com;email4@domain.com"
.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
编辑: 我从
更改了此代码位If InStr(olMail.Subject, "Exposure Statement - COB date") <> 0 Then
到
If olMail.SenderEmailAddress = "email1@gdomain.com" And olMail.ReceivedTime = Now() Then
但是不行...
这是唯一能让我找到准确邮件的搜索组合(SenderEmailAddressthat 和 ReceivedTime)...
您应该使用:工具->参考。找到 Microsoft Outlook 15.0 Object Library
,检查它并关闭 window。
或者只使用后期绑定
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