Outlook Signature Insertion Using Excel VBA

Anne Troy

MrExcel MVP
Joined
Feb 18, 2002
Messages
2,632
Office Version
  1. 365
Platform
  1. Windows
Got the code from Ron de Bruin, but can't seem to make it work. The whole thing was working good so far, though I am not really done with the body. I added the signature in every possible way, but it simply doesn't put my signature in. Yes, the file exists in the path given. He used the "environ" string, but I hard-coded it just to see if that was part of the problem, and it changed nothing.

Code:
Function GetBoiler(ByVal sFile As String) As String
'**** Kusleika
    Dim fso As Object
    Dim ts As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
    GetBoiler = ts.readall
    ts.Close
End Function


Sub TestFile()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim cell As Range
    Dim strbody As String
    Dim SigString As String
    Dim Signature As String


    Application.ScreenUpdating = False
    Set OutApp = CreateObject("Outlook.Application")

    On Error GoTo cleanup
    For Each cell In Rows("3").Cells.SpecialCells(xlCellTypeConstants)
        If cell.Value Like "?*@?*.?*" Then
       ' And _
        '   LCase(Cells(cell.Row, "C").Value) = "yes"
        

            Set OutMail = OutApp.CreateItem(0)
            On Error Resume Next
            With OutMail
                .To = cell.Value
                .Subject = "POS Order Update: " & Range("D7").Text & " " & Range("E7").Text
                .Body = "Hi, " & Range("c7").Text & "!" & Chr(10) & Chr(10) & _
                Range("D10").Text & " has been assigned to program your Point of Sale database."
                
                .Display  'Or use Send
                SigString = "C:\Users\atroy\AppData\Roaming\Microsoft\Signatures\AnneTroyHT.htm"

End With
            On Error GoTo 0

    If Dir(SigString) <> "" Then
        Signature = GetBoiler(SigString)
    Else
        Signature = ""
    End If

  Set OutMail = Nothing
        End If
    Next cell

cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True


End Sub

Help!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi Anne,
You haven't appended the Signature string to the Body property. You need to get the signature first and then tack it onto the end of the Body text.
 
Upvote 0
Thanks, Rory. I don't do VBA. I just yoink it. Any suggestions as to HOW I append it? I think I understand what you mean, but all the other components have a dot, like .To and such. How do I get it to use SigString?

And how do I get the String FIRST?
 
Upvote 0
Something like this:
Code:
Function GetBoiler(ByVal sFile As String) As String
'**** Kusleika
    Dim fso As Object
    Dim ts As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
    GetBoiler = ts.readall
    ts.Close
End Function


Sub TestFile()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim cell As Range
    Dim strbody As String
    Dim SigString As String
    Dim Signature As String

    Application.ScreenUpdating = False
    Set OutApp = CreateObject("Outlook.Application")
                SigString = "C:\Users\atroy\AppData\Roaming\Microsoft\Signatures\AnneTroyHT.htm"
    If Dir(SigString) <> "" Then
        Signature = GetBoiler(SigString)
    Else
        Signature = ""
    End If
    On Error GoTo cleanup
    For Each cell In Rows("3").Cells.SpecialCells(xlCellTypeConstants)
        If cell.Value Like "?*@?*.?*" Then
       ' And _
        '   LCase(Cells(cell.Row, "C").Value) = "yes"
        

            Set OutMail = OutApp.CreateItem(0)
            On Error Resume Next
            With OutMail
                .To = cell.Value
                .Subject = "POS Order Update: " & Range("D7").Text & " " & Range("E7").Text
                .Body = "Hi, " & Range("c7").Text & "!" & Chr(10) & Chr(10) & _
                Range("D10").Text & " has been assigned to program your Point of Sale database." & vbLf & Signature
                
                .Display  'Or use Send
End With
            On Error GoTo 0

  Set OutMail = Nothing
        End If
    Next cell

cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True


End Sub
 
Upvote 0
Thanks very much. That gives me a bunch of garbage html code or, if I change it to a TXT signature, I get plain text.

But what it really did was make me realize I don't need to hold up this mini app for a signature. The fact that it creates the email much faster than manually makes it more efficient, even if I have to manually insert my signature using Outlook.

Thanks a ton!!
 
Upvote 0
Is your email being created in HTML, rtf or plain text format?
What you might do, which IMO is easier with sigs, is to create a template email with your signature, then use the Outlook Application's CreateItemFromTemplate method instead.
 
Upvote 0
Ron de Bruin is pretty much the VBA email God, and he has some information here:

http://www.rondebruin.nl/mail/folder3/signature.htm

Specifically, this:

In Vista or Win 7 use this

Code:
SigString = "C:\Users\" & Environ("username") & _
            "\AppData\Roaming\Microsoft\Signatures\Mysig.txt"

Now, from what I understand, you should change "mysig.txt" to be the desired signature YOU want that exists in that folder. For instance, my sig name is annetroy.txt plain text format or annetroy.rtf for rich text format or annetroy.htm for html format. I don't know how you could actually get the user's file, since that would change according to the user, but perhaps if everyone used the same names for their sigs, such as "mysig" like the code indicates, you could write some VBA for use by everyone.
 
Upvote 0
Hi Rorya,
Instead of the .txt signature, is there a way to invoke a user's .htm signature to capture any graphics or formatted text?
 
Upvote 0

Forum statistics

Threads
1,224,513
Messages
6,179,212
Members
452,895
Latest member
BILLING GUY

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top