0

Since I am new to the VBA I am trying to structure a code to send email through outlook with the signature.But when I run the below code it displays me only the signature (body does not appear).

When I use "F8" and check it can be clearly seen that my body appears then signature overwrites it.How do I fix this?

Option Explicit

Sub SendEmailwithsign()
Dim objoutApp As Object, objoutmail As Object
Dim strbody As String, strSig As String

Set objoutApp = CreateObject("outlook.Application")
Set objoutmail = objoutApp.CreateItem(0)
On Error Resume Next
With objoutmail
  .To = "AAAAAAAAA@.com"
  .CC = ""
  .Subject = "Test mail"
  .Recipients.ResolveAll
  .Display                 'body appears until this point'

  strSig = .Environ("appdata") & "Microsoft\Signatures\bbbb.txt"

  strbody = "Hello"
  .body = strbody & strSig     'with this step Body part does not appear but only the signature'

  End With
   On Error GoTo 0
   Set objoutmail = Nothing
   Set objoutApp = Nothing

End Sub
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Nilusha M.
  • 57
  • 1
  • 13

1 Answers1

1

You need to change .body to .HTMLBody, please refer to the below code:

Option Explicit

Sub SendEmailwithsign()
Dim objoutApp As Object, objoutmail As Object
Dim strbody As String, strSig As String

Set objoutApp = CreateObject("outlook.Application")
Set objoutmail = objoutApp.CreateItem(0)
On Error Resume Next
With objoutmail
  .To = "emailAddress"
  .CC = ""
  .Subject = "Test mail"
   strSig = .Environ("appdata") & "Microsoft\Signatures\default.txt"
  strbody = "Hello"
  .HTMLBody = "<p>"+ strbody  +"</p>" & strSig     'with this step Body part does not appear but only the signature'
  .Recipients.ResolveAll
  .Display                 'body appears until this point'



  End With
   On Error GoTo 0
   Set objoutmail = Nothing
   Set objoutApp = Nothing

End Sub

Reference from:

VBA Signature on Email with HTMLBody

Alina Li
  • 884
  • 1
  • 6
  • 5
  • I really appreciate your contribution,but my problem has not been solved yet with the above code.Same incident happens when it goes through ".HTMLBody" part body overwrites the signature and finally remain with only the body.How do I solve this? – Nilusha M. Nov 19 '18 at 08:29
  • Maybe these links will help for you: https://forums.windowssecrets.com/showthread.php/163512-Add-Signature-to-HTML-email-using-VBA https://www.mrexcel.com/forum/excel-questions/358574-add-outlook-signature-using-vba-when-sending-email.html – Alina Li Nov 19 '18 at 09:09
  • Got it.I will try with these links but can I know what is the reason behind that it works in your machine and it does not work my machine? – Nilusha M. Nov 19 '18 at 09:23
  • I'm not sure. Is it possible the environment is diffirent. This is my enviroment: windwo10 64Bit +office2016 professional. – Alina Li Nov 19 '18 at 10:17