Excel VBA, how to Reply to a specific email message

回眸只為那壹抹淺笑 提交于 2019-12-07 17:09:26

问题


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"

Example #2 of subject "Margin Notice COB 2015-Feb-10"

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 = "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

Edit: I changed this code bit from

If InStr(olMail.Subject, "Exposure Statement - COB date") <> 0 Then

to

If olMail.SenderEmailAddress = "email1@gdomain.com" 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...


回答1:


You shoud use: Tools->References. Find Microsoft Outlook 15.0 Object Library, check it and close the window.




回答2:


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


来源:https://stackoverflow.com/questions/28612018/excel-vba-how-to-reply-to-a-specific-email-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!