0

my initial problem was inserting a signature including its format, I have managed to fix it but, i have a problem with an additional signature inserted between the xmailbody1 and the pasted table, see code below:

Sub SendEmail()
    Dim MyRange As Range
    Dim doc As Object, x, signature, xMailBody2, xMailBody1
    Dim lastRow As Long
    
    Set MyRange = Selection
    With CreateObject("outlook.application").CreateItem(0)
        .Display   'Change to .Send to send the email immediately
        
    signature = .HTMLBody

        Set doc = .GetInspector.WordEditor
        
        xMailBody1 = "<BODY>Hi Mike, <br><br>This is the first paragraph</BODY>"
                    
        x = doc.Range.End - 1
        MyRange.Copy
        doc.Range(x).Paste
        
        xMailBody2 = "<BODY><br>Thank you for your assistance. Please let me know if you require any further information.<br> <br>" & _
                    "Best regards,</BODY>"
        
        .HTMLBody = xMailBody1 & .HTMLBody & xMailBody2 & signature
                    
        .To = "someone@somewhere.com"
        .Subject = "Subject"
        
        Application.CutCopyMode = 0
    End With
    
End Sub

---current pic---

enter image description here

---desired pic---

enter image description here

niton
  • 8,771
  • 21
  • 32
  • 52
k1dr0ck
  • 1,043
  • 4
  • 13
  • https://stackoverflow.com/a/54967553/8422953 – braX Apr 06 '23 at 04:01
  • @braX thanks, i have already inserted the signature with its format but my new code cant get the order of text body and table right – k1dr0ck Apr 06 '23 at 04:06
  • You'll need to save the signature and body text somewhere separate before putting the contents into the `HTMLbody` in the order you desire. Have a look here where I did the same thing https://stackoverflow.com/a/75797317/1727575 – Kairu Apr 06 '23 at 05:00
  • @Kairu corrected the order but cant figure out how not to show the signature between xmailbody1 and pasted table – k1dr0ck Apr 07 '23 at 00:12
  • 1
    Try setting ‘.HTMLbody = “”‘ before pasting the table – Kairu Apr 07 '23 at 04:19

1 Answers1

0

solved as suggested by @Kairu using .HTMLBody = "" before the pasting the table

open to other solutions better than below code

Sub SendEmail()
    Dim MyRange As Range
    Dim doc As Object, x, signature, xMailBody2, xMailBody1
    Dim lastRow As Long
    
    Set MyRange = Selection
    With CreateObject("outlook.application").CreateItem(0)
        .Display   'Change to .Send to send the email immediately
        
    signature = .HTMLBody

        Set doc = .GetInspector.WordEditor
        
        xMailBody1 = "<BODY>Hi Mike, <br><br>This is the first paragraph</BODY>"
        
        .HTMLBody = ""

        x = doc.Range.End - 1
        MyRange.Copy
        doc.Range(x).Paste
        
        xMailBody2 = "<BODY><br>Thank you for your assistance. Please let me know if you require any further information.<br> <br>" & _
                    "Best regards,</BODY>"
        
        .HTMLBody = xMailBody1 & .HTMLBody & xMailBody2 & signature
                    
        .To = "someone@somewhere.com"
        .Subject = "Subject"
        
        Application.CutCopyMode = 0
    End With
    
End Sub
k1dr0ck
  • 1,043
  • 4
  • 13