Back to jkp-ads.com |
Ron de Bruin
|
Ron de Bruin decided to remove all Windows Excel content from his website for personal reasons. If you want to know why, head over to rondebruin.nl.
Luckily, Ron was kind enough to allow me to publish all of his Excel content here.
Most of these pages are slightly outdated and may contain links that don 't work. Please inform me if you find such an error and I'll try to fix it.
Kind regards
Jan Karel Pieterse
Important read this :
The code on this page is only working when you use Outlook as your mail
program.
Copy the code in a Standard module of your workbook, if you just
started with VBA see this page.
Where do I paste
the code that I find on the internet
Check out this Tip page for changing the code on this page.
Tips for changing the code examples
This is the easiest way to add the default signature with or without picture to a mail with VBA code. The only problem is that if you want to send the mail directly you see the screen flicker because we must display the mail for a short time before the code send it. No problem if you want to check out the mail first before you send it, but in a loop this is not so nice. I have not find a better way to send a signature with a picture in a mail directly without showing the mail for a short time. The second example not have this problem but can't handle a signature with a picture in it.
Note : You only have to change the mail address before you run the code
Sub Mail_Outlook_With_Signature_Html_1()
' Working in Office 2000-2016
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "<H3><B>Dear Customer Ron de Bruin</B></H3>" & _
"Please visit this website to download the new version.<br>" & _
"Let me know if you have problems.<br>" & _
"<A HREF=""/rdb/tips.htm"">Ron's Excel Page</A>" & _
"<br><br><B>Thank you</B>"
On Error Resume Next
With OutMail
.Display
.To = "ron@debruin.nl"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.HTMLBody = strbody & "<br>" & .HTMLBody
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
If you create a signature in Outlook it will save three files (HTM,
TXT and RTF) into
Vista and Windows 7/8:
C:\Users\<UserName>\AppData\Roaming\Microsoft\Signatures
Windows XP :
C:\Documents and
Settings\<UserName>\Application Data\Microsoft\Signatures
Note: "Application Data" and "AppData" are hidden folders,
change the view in Windows explorer so it show hidden files and folders if
you want to see the files.
In the mail macro below we use the HTM
file. You only must change the file name of the signature in the code to
your signature name. In Outlook you can see the name of every signature you
have, this is also the name of the signature file so you not have to look in
the signature folder for it. I use the signature name Mysig in
the
example below. The code will find the correct path for you so that is easy.
Important : The code will not add the signature if you
try the code in Excel 2000-2003 and Word is your mail editor, you can turn
this setting of in the Outlook 2000/2003 options if you want. No problems
when you run the code in Excel 2007-2016.
Note : You only have to change the mail address and name of your signature file before you run the code
Sub Mail_Outlook_With_Signature_Html_2() ' Don't forget to copy the function GetBoiler in the module. ' Working in Office 2000-2016 Dim OutApp As Object Dim OutMail As Object Dim strbody As String Dim SigString As String Dim Signature As String Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) strbody = "<H3><B>Dear Customer Ron de Bruin</B></H3>" & _ "Please visit this website to download the new version.<br>" & _ "Let me know if you have problems.<br>" & _ "<A HREF=""/rdb/tips.htm"">Ron's Excel Page</A>" & _ "<br><br><B>Thank you</B>" 'Change only Mysig.htm to the name of your signature SigString = Environ("appdata") & _ "\Microsoft\Signatures\Mysig.htm" If Dir(SigString) <> "" Then Signature = GetBoiler(SigString) Else Signature = "" End If On Error Resume Next With OutMail .To = "ron@debruin.nl" .CC = "" .BCC = "" .Subject = "This is the Subject line" .HTMLBody = strbody & "<br>" & Signature .Send 'or use .Display End With On Error GoTo 0 Set OutMail = Nothing Set OutApp = Nothing End Sub Function GetBoiler(ByVal sFile As String) As String 'Dick 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
In the two macros above we use .HTMLBody to add text and a signature to the mail but what if you want to use .Body to create a plain message ?
Example 1 : Mail_Outlook_With_Signature_Html_1
Change the strbody string to
strbody = "Hi there" & vbNewLine & vbNewLine & _ "This is line 1" & vbNewLine & _ "This is line 2" & vbNewLine & _ "This is line 3" & vbNewLine & _ "This is line 4"
Change the .HTMLBody line to
.Body = strbody & vbNewLine & .Body
Example 2 : Mail_Outlook_With_Signature_Html_2
Change the strbody line to
strbody = "Hi there" & vbNewLine & vbNewLine & _ "This is line 1" & vbNewLine & _ "This is line 2" & vbNewLine & _ "This is line 3" & vbNewLine & _ "This is line 4"
Change the .HTMLBody line to
.Body = strbody & vbNewLine & Signature
Also change the extension(htm) of the signature file named MySig.htm in the SigString to txt
If you want to use the Intellisense help showing you the properties and
methods of the objects as you type you can use Early Binding.
Bit faster also when you run your code but you can have problems when you
distribute your workbooks. Excel will automatic update the reference number
to Outlook when you open your workbook in a higher version of Excel/Outlook
but not update it when you open it in a lower version of Excel/Outlook. With
Late Binding as I used in the macro examples you not have
this problem.
Add a reference to the Microsoft Outlook Library in
Excel
1) Go to the VBA editor with the shortcut
Alt - F11
2) Click on
Tools>References in the Menu bar
3) Place a
Checkmark before Microsoft Outlook ? Object Library
Where ? is the Outlook version number
Then replace
this three lines in the code
Dim OutApp As
Object
Dim OutMail As Object
Set OutMail = OutApp.CreateItem(0)
With this three lines
Dim OutApp As
Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutMail =
OutApp.CreateItem(olMailItem)