0
Sub Display()
Dim myMail As Outlook.MailItem
Dim myReply As Outlook.MailItem
Dim numItems As Integer
Dim mySelected As Selection
Dim i As Integer
Dim myText As String
Dim signature As String

signature = Environ("appdata") & "\Microsoft\Signatures\"
If Dir(signature, vbDirectory) <> vbNullString Then
    signature = signature & Dir$(signature & "*.htm")
Else:
    signature = ""
End If
signature = CreateObject("Scripting.FileSystemObject").GetFile(signature).OpenAsTextStream(1, -2).ReadAll
Set mySelected = Outlook.ActiveExplorer.Selection
numItems = mySelected.Count

For i = 1 To numItems
Set myMail = mySelected(1)
Set myReply = myMail.Reply
myText = myMail.Body
myReply.Subject = "RO Finalized WF: Annual Review. Entity"
myText = "Hi All," & vbCrLf & vbCrLf & "Worflow ID:" & vbCrLf & vbCrLf & "infoinfoinfoinfo" & vbCrLf & vbCrLf & "Thanks," & vbCrLf & "Josh" & signature
myReply.HTMLBody = myText & vbCrLf & vbCrLf & myMail.HTMLBody 
Myreply.display
  Set myMail = Nothing
  Set myReply = Nothing
Next
Set mySelected = Nothing
End Sub

The code above displays a reply to the email you currently have open in Outlook including who sent it (placed in To:) with the whole body of the email you currently have open in Outlook.

This is what I want it to do except instead of replying to the open email, I want it to reply to the email specifically by it's subject. Also I want it to include exactly what all replies include in Outlook (the line separating each email, with the From:, Sent,: To:, CC:, Subject: of the previous email showing). Also vbCrLf is not doing it's purpose after MyText.

I would also like it to place the CC: from the previous email in the CC of the email I am creating.

I am not an expert in VBA and have tried as much as I could think of. Thank you for the help in advance :)

I have found another option and the code is displayed below. This will populate a reply email, with everything I need except my customized body.

Sub Display()

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

Dim IsExecuted As Boolean

 signature = Environ("appdata") & "\Microsoft\Signatures\"
 If Dir(signature, vbDirectory) <> vbNullString Then
    signature = signature & Dir$(signature & "*.htm")
Else:
    signature = ""
End If
signature = CreateObject("Scripting.FileSystemObject").GetFile(signature).OpenAsTextStream(1, -2).ReadAll
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
IsExecuted = False
For Each olMail In Fldr.Items
If InStr(olMail.Subject, "checklist") <> 0 Then
If Not IsExecuted Then
        With olMail.ReplyAll

    .HTMLBody = "Dear All," & "<br>" & signature


        End With
    IsExecuted = True
    olmail.ReplyAll.Display
End If
End If
Next olMail
End Sub

Solution

Sub Display()
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Variant
Dim i As Integer
Dim IsExecuted As Boolean
signature = Environ("appdata") & "\Microsoft\Signatures\"
If Dir(signature, vbDirectory) <> vbNullString Then
signature = signature & Dir$(signature & "*.htm")
Else:
signature = ""
End If
signature = CreateObject("Scripting.FileSystemObject").GetFile(signature).OpenAsTextStream(1, -2).ReadAll
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)

IsExecuted = False
For Each olMail In Fldr.Items
If InStr(olMail.Subject, "Subject") <> 0 Then
If Not IsExecuted Then

    With olMail.ReplyAll

.HTMLBody = "<p>" & "Dear All," & "</p><br>" & signature & .HTMLBody

.Display
  End With
IsExecuted = True


End If
End If
Next olMail
End Sub
Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
Tmacjoshua
  • 79
  • 1
  • 13
  • Firstly, you cannot concatenate multiple HTML strings and expect valid HTML. HTML strings must be merged (which can be far from trivial). To get the CC recipients as well, use ReplyAll instead of Reply. – Dmitry Streblechenko Jul 04 '18 at 06:52
  • Thank you for the reply Dmitry Streblechenko. Would you mind displaying the code so I can understand what you mean. I am new to VBA :) – Tmacjoshua Jul 04 '18 at 12:42
  • Does anyone know of another forum I could ask this question, as it is time sensitive. Thanks! – Tmacjoshua Jul 10 '18 at 19:37
  • What have you tried and what exactly does not work? – Dmitry Streblechenko Jul 10 '18 at 20:11
  • The furthest I got with this was using the codes lines above. I have tried all that I could find with the knowledge of VBA I have, which is not too experienced (Started about a month ago). In the new code lines I posted, I can't seem to figure out how to have the email content I want to reply, in the body of the email I am displaying. I have tried (.bodyformat = olformathtml) after (with olMail.ReplyAll) in the new lines of coding. This opens up the email I want to reply to, but I can not input my own custom body. – Tmacjoshua Jul 10 '18 at 20:31
  • [Outlook Reply or ReplyAll to an Email](https://stackoverflow.com/a/31818677) demonstrates how to add to the htmlBody when replying. Ignore the use of vbCrLf it is not proper html. In the update to [Outlook Email and Signature from Excel VBA - .Body vs .HTMLbody](https://stackoverflow.com/q/36211195) the OP demonstrates the use of
    instead of vbCrLf.
    – niton Jul 10 '18 at 23:43
  • Unfortunately that code does not work for me. I have tried it alone as well as combining it with my code and was unsuccessful. All that I can not figure out now is the customized body. I have the line of code " With olMail.ReplyAll .HTMLBody = "Dear All," & "
    " & signature" but it is ignored.
    – Tmacjoshua Jul 11 '18 at 14:35
  • @Tmacjoshua that was a hint that you had from Dimitry as well to use all html not some. `.HTMLBody = "

    " & "Dear All, " & "


    " & signature & .HTMLBody` as well move the `.Display` inside the With. To respond non-owners of the post in a comment put an @ before the username.
    – niton Jul 11 '18 at 17:19
  • @niton your help was much appreciated! That is the solution. If there is another email with the same subject, is there a way to pin point a specific email. In my case, occasionally someone will reply to the email and CC me, causing this macro to reply to that one instead of the one intended. – Tmacjoshua Jul 11 '18 at 18:25
  • `If InStr(olMail.To, "your name as it appears in the To line") Then` – niton Jul 11 '18 at 20:04
  • @niton Can you explain what this code does, not quite sure how to use it? The code will be used by numerous people. Is there a way to pin point a specific email if there are numerous emails with the same subject? – Tmacjoshua Jul 12 '18 at 17:57
  • @niton to be more clear. My team and I are sent emails once another team completes their work. Sometimes there are questions/changes asked in an email with the same subject but not the same addresses as the first email (the email we reply to). Is there a way for the macro to reply to the specific macro. If not grab all the addresses from all the emails with the specific subject. – Tmacjoshua Jul 12 '18 at 18:08
  • I suggest then the subsequent subjects would have "Re:" in front of the subject. If this still does not address the issue, try formulating a new question, with applicable code, sample mail... – niton Jul 12 '18 at 19:08
  • It occurred to me after all the confusion caused by asking two question in the same post you may want current active item https://stackoverflow.com/questions/4134967/how-do-you-get-a-reference-to-the-mail-item-in-the-current-open-window-in-outloo or selecton https://stackoverflow.com/questions/45485283/how-to-save-selected-item-in-outlook-vba. – niton Jul 18 '18 at 22:10
  • @niton Thankyou. I have one more question adding onto my solution. So the email I have picks of all the recipients, but is it possible to have the CC pick up all the recipients but also be able to add an if then statement to add in additional recipients. – Tmacjoshua Jul 19 '18 at 17:31
  • You may post a separate question with adequate explanation and code. – niton Jul 19 '18 at 20:46
  • Ok @niton will do thanks. With regards to this question, the email that is being generated is the first email that contains the text input into the specified cell. For example, if I want to reply to the email with the subject "Checklist" it will grab the first email with the word checklist in the subject. Do you know what I should change in the code to have it grab the most recent or a specific email? Your help is much appreciated Niton really. – Tmacjoshua Jul 19 '18 at 21:24

0 Answers0