Excel VBA,如何回复特定的电子邮件

时间:2015-02-19 16:47:08

标签: excel vba email outlook reply

我每个星期三都会收到一封来自特定发件人的邮件。此电子邮件的主题有时会更改

主题示例#1"曝光声明 - COB 20150217"

主题示例#2"保证金通知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)......

2 个答案:

答案 0 :(得分:2)

您应该使用:工具 - &gt;参考。找到Microsoft Outlook 15.0 Object Library,检查并关闭窗口。

答案 1 :(得分:1)

或者只是使用后期绑定

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