2
votes

I am trying to "replytoall" with a given format in the Body.

I use the following code to search for and display the mails.

Sub Test()

Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Variant
Dim i As Integer

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
i = 1

For Each olMail In Fldr.Items
If InStr(olMail.Subject, "Application for Privilege Leave - Leave ID - Dev-PL-45252-4") <> 0 Then
olMail.Display

i = i + 1
End If
Next olMail
End Sub

I need to Replyall with the same subject and a prescribed body and signature.

It is similar to when we open up a mail in Outlook and click on the Reply to All button.

I want it triggered from Excel.

3

3 Answers

1
votes

Since you are using Early Binding, Change

Dim olMail As Variant

to

Dim olMail As Outlook.MailItem

And then you will be able to access all the properties of the olMail item. One of which is .ReplyAll

ScreenShot

enter image description here

If InStr(olMail.Subject, "Blah Blah") <> 0 Then
    olMail.Display
    olMail.ReplyAll

    DoEvents

    '
    '~~> Rest of the code
    '

    i = i + 1
End If
0
votes

There is a ReplyAll method which returns a mail object. See here.
So if you are iterating through some mails, then this should work:

For Each oMail in Fldr.Items
    If InStr(olMail.Subject, "mysubject") <> 0 Then
        With oMail.ReplyAll
            .Subject  = oMail.Subject '~~> this is optional
            .Body = "your Body"
            '~~> all other stuff you need your mail to have
            .Display '~~> change to .Send if it is already ok
        End With
    End If
Next

Not tested but should be close.

0
votes

Try this one:

olMail.ReplyAll
olMail.ReplyAll.body = bodyMail & vbLF & .body