0

I have this code to a vba outlook macro to reply all.

Sub my_test()

Dim objItem As Object

Dim mail As MailItem
Dim replyall As MailItem

Dim templateItem As MailItem

For Each objItem In ActiveExplorer.Selection

    If objItem.Class = olMail Then
    
        Set mail = objItem
        Set replyall = mail.replyall
                
        Set templateItem = CreateItemFromTemplate("C:\template.oft")
        
        With replyall
            .HTMLBody = templateItem.HTMLBody & .HTMLBody
            .Display
        End With
        
    End If
    
Next

End Sub

I am trying to add a functionality so that when the original email brings an attachment (docx, pdf), when I reply all using this macro it will also use the original attachment and place it as an attachment in the reply all email. How can I achieve this?

H.N.
  • 1,207
  • 2
  • 12
  • 28
  • Do you intend running the above code from Outlook application, or like an automation from Excel, Word etc. VBA? – FaneDuru Feb 11 '21 at 10:36
  • The idea was to use this code. Thanks – H.N. Feb 11 '21 at 12:05
  • The idea was in which application do you need using it? Did you test my answer code? – FaneDuru Feb 11 '21 at 12:14
  • Is it my question unclear? From what application do you want running/run your "this code"? I am asking only to clarify the paths of the attachments to be temporarily saved, according to the specific application where the code should run. I can use an independent path, anyhow, but in the way I tried designing it you do not have to adapt anything... Don't you run "this code" from Excel VBA? – FaneDuru Feb 11 '21 at 12:51
  • I changed my initial answer and made it to use your code with its way of variable declarations. Please, test it and confirm that it works as you requested. – FaneDuru Feb 11 '21 at 13:01
  • I replied below. Thanks for your help but it is not working as I which – H.N. Feb 11 '21 at 17:59
  • I do not see any reply "below". But saying "is not working as I which" does not help at all. Does the code raise any error? **What** does not work as you wish? What does it do against you want? The code philosophy is to save the existing attachments of the selected mail, attach them to the new mail to be sent and delete them after that. Was the folder Did you try running the code line by line and understand what it does? Was the folder "C:\outlook-attachments created? I tested and it work on my laptop. Can't you see the mail preview with its attachment(s)? – FaneDuru Feb 11 '21 at 18:25
  • I replied below. – H.N. Feb 11 '21 at 18:51
  • Do you know if, when i use this macro, the email gets marked as replied? I am asking this because in the original macro code it does – H.N. Feb 11 '21 at 18:52
  • Do you try asking me? If yes, you should tag me (using @FaneDuru). If not, please tag the one you want addressing to. You said that my code did not work. Even if it works on my laptop. My code does exactly what your "original" code did, from this point of view and also add existing attachments, if any. – FaneDuru Feb 11 '21 at 19:01
  • Read what I write bellow. Yes it was to you. Did you saw anyone else replying?! – H.N. Feb 11 '21 at 19:03
  • I do not understand what that "bellow" does mean. I do not see anything connected to what you say. If you are talking about my code, you should comment below my code. There are two answers. You only comment below the other one and I thought that you maybe need to talk to the other one posting an answer. But what does it cost you to test the code and see what it does and what it does not? – FaneDuru Feb 11 '21 at 19:04
  • When you added this ".To = mail.replyall.To" it worked. Now can you awnser me? Does the email gets marked as replied? – H.N. Feb 11 '21 at 19:10
  • I did not added anything, because it is not necessary. I am afraid you are confusing the answers. If the other answer uses forward, it cannot mark it as replied. And my code should marked it as replied, because it is a replay. But I cannot understand why you ask it instead of testing by yourself... – FaneDuru Feb 11 '21 at 19:17

1 Answers1

1

Forward then populate the .To with what would appear in a ReplyAll.

Option Explicit

Sub my_test()

Dim objItem As Object

Dim mail As MailItem
Dim forwardMail As MailItem

Dim templateItem As MailItem

For Each objItem In ActiveExplorer.Selection

    If objItem.Class = olMail Then
    
        Set mail = objItem
        Set forwardMail = mail.Forward
        
        Set templateItem = CreateItemFromTemplate("C:\template.oft")
        
        With forwardMail
            .HTMLBody = templateItem.HTMLBody & .HTMLBody
            '.To = mail.replyall.To
            .To = mail.replyall.To & ";" & mail.replyall.CC
            .Display
        End With
        
    End If
    
Next

End Sub
niton
  • 8,771
  • 21
  • 32
  • 52
  • WIth this approach, as I am forwarding the email, I dont get the contacts which I have in the original email. I'd like also to keep the contacts so when I send this reply the attachment also goes to everyone in the original email. Can you help? – H.N. Feb 11 '21 at 17:57
  • ALmost there :) THis puts the sender and the contacts which are in to in my email. But still missing the contacts which come in CC (original email). – H.N. Feb 11 '21 at 18:10
  • I think this works: .To = mail.replyall.To & mail.replyall.CC – H.N. Feb 11 '21 at 18:13