0

When I .Display mail, to add the signature, two empty lines are added above the signature. Is there a way to remove them, to make the mail look better, without losing the signature formatting?

With objOutlookMsg
    .SentOnBehalfOfName = "test@stackoverflow.com"
    .To = "test@stackoverflow.com"
    .CC = "test@stackoverflow.com"
    .Subject = "Stackoverflow"

    ' Tekst
    .Display 
    .HTMLBody = "<p style='font-family:arial;font-size:13'>" & _
    "Hej" & "</p>" & _
    vbNewLine & vbNewLine & _
    "<p style='font-family:arial;font-size:13'>" & _
    "Test Test Test Test Test Test" & "</p>" & _
    .HTMLBody

    .Display
End With

Picture of mail and signature to show, that there are no empty lines at the beginning.

enter image description here

enter image description here

enter image description here

Community
  • 1
  • 1
  • Are you sure the linebreaks are not in the signature itself? – Nacorid Jul 16 '19 at 12:43
  • I added two pictures to show that there are no empy lines in the signature. I assume Outlook simply add the signature two lines below the start of the mail. – Dennis Christiansen Jul 16 '19 at 12:49
  • So, its generally accepted to have space between the signature and the body of the email, so I'm not sure where the problem is in that. If you don't actually want any body in the email and ONLY the signature detalis, add the signature details as the HTML body instead of leaving it up to outlook. There isn't a way to directly interface with the signature and vba. The signature is created at mail creation time-AFTER-the vba has generated the email. In short - either insert the signature manually in the vba (via HTMLBody) or live with the lines. – Mike Jul 16 '19 at 15:14
  • @Mike Look at the picture above. I need to add a short text to the mails, but as you can see there are to blank lines between the body and signature, which looks wierd in short a short mail. I'm not sure I understand the part about the signature? As far as I can see, when the .Display is placed before the body, the mail is created, and then VBA insert the body into the mail, meaning that the two blank lines are pushed down creating this blank space. – Dennis Christiansen Jul 16 '19 at 15:27
  • I've seen some solutions, where the signature is found in AppData but this has to work for multiple people and the names of the signatures are to different for this to work. – Dennis Christiansen Jul 16 '19 at 15:34
  • Removed my answer as your requirements need HTML. Also, I just stumbled across this post - https://stackoverflow.com/questions/8994116/how-to-add-default-signature-in-outlook - which provides a similar solution as mine as well as multiple solutions utilizing the app data approach, which should be consistent throughout the diff users as APPDATA is a system variable and assigned to the user account. - Should be a safe approach pulling the HTM or RTF files from there. If you need help adjusting one of those solutions to your issue, let me know. – Mike Jul 17 '19 at 14:44
  • Thank you for the help, but thoes solutions wont work, I've already teste it. The formattation of the signatures are lost. The problem with finding the signatures in the folder is that our signatures are created with an add-in. So the name depends on the department, chosen language and incldues our entire name (compared to only our initials that our windoes user use). So the code would have to an HTML file in the AppData folder without knowing the name beforehand. – Dennis Christiansen Jul 18 '19 at 06:20

1 Answers1

0

It's been a few years since you asked this, I just struggled with the same issue, where I wanted the signature to work regardless of who is sending. I figured I would still share here in case anyone else is having similar issues.

I'm not a great coder, but I was able to piece things together through a bunch of different articles/posts to get something working, so there is likely a more efficient way to execute this.

To get the signature, it effectively displays an empty e-mail with default signature, removes the white space, saves it, and then empties and discards the e-mail before moving to the final e-mail send (on my comp just discarding left the signature at the top of the final email). The signature is attached at the end of that e-mail, .HTMLBody is used for both instances.

Here is the full code with 'default' inputs, for anyone else that may stumble across:

Sub sig_and_email_send()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim signature As String

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

  'gets default signature from e-mail
    With OutMail
        '2 = HTMLBody Format
        .BodyFormat = 2
        .Display
        'deletes blank space present before signature
        signature = Replace(OutMail.HTMLBody, "<p class=MsoNormal><o:p>&nbsp;</o:p></p>", "")
        'removes entire e-mail contents and then closes with discard
        OutMail.HTMLBody = Replace(OutMail.HTMLBody, OutMail.HTMLBody, "")
        OutMail.Close 1
    End With
    
    On Error Resume Next
    With OutMail
        .to = "email@email.com"
        .CC = "email@email.com"
        .BCC = "email@email.com"
        .Subject = "Subject"
        .Attachments.Add "\\network\folder\documents\file.xlsx"
        .HTMLBody = .HTMLBody & "Hi," & "whateveryourtextis" & _
        "<br>" & "Thanks," & "<br><br>" & signature
        .Display 'or use .Send
    End With
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
procopypaster
  • 416
  • 1
  • 6