Solved

Excel VBA create Outlook Email format

Posted on 2010-08-23
6
1,012 Views
Last Modified: 2012-05-10
Hi Experts

When I normally use Outlook and click New Mail Message, I get a formatted blank email which has a signature, default font, business card etc.

My VBA below successfully creates an Outlook email from Excel.  The email it creates is unformatted and doesn't have the signature etc as described above.

My guess is it's something to do with the way the VBA creates the mail using the CreateObject command, or something.

So my question is, how do I alter the code so that the VBA creating an email mimics the New Mail Message button in Outlook?  Then I can have nice looking emails.

I hope that's clear.

Regards,

Will
Sub SendOutlookInvoice()

Dim ws As Worksheet, sTo As String, sAmount As String, sDueDate As String, sSubject As String

Dim oOutlookApp As Object, oOutlookMail As Object

    

Set oOutlookApp = CreateObject("Outlook.Application")

oOutlookApp.Session.Logon

    

Cells(5, ActiveCell.Column).Select



Application.ScreenUpdating = False

    

Set ws = ActiveSheet

    

With ws

    sTo = ActiveCell.Offset(52, 0).Value

    sAmount = ActiveCell.Offset(11, 0).Value

    sDueDate = ActiveCell.Value

    sSubject = ActiveCell.Offset(-2, 0).Value

    sContact = ActiveCell.Offset(51, 0).Value

    sHosting = ActiveCell.Offset(53, 0).Value

End With

    

Set oOutlookMail = oOutlookApp.CreateItem(0)

On Error Resume Next

With oOutlookMail

    .To = sTo

    .CC = ""

    .BCC = ""

    .Subject = "Annual renewal of website www." & sSubject

    .Body = "Dear " & sContact & vbCrLf & vbCrLf & "Your website www." & sSubject & " annual " & sHosting & " is due for renewal on " & sDueDate & "." & vbCrLf & vbCrLf & "Please find attached an invoice for $" & sAmount & " for one years renewal of these services." & vbCrLf & vbCrLf & "This email is automatically generated. Please feel free to contact us should you need help. If you consider this invoice is incorrect, or you have been wrongly sent this mail, please contact us." & vbCrLf & vbCrLf & "Croxford Technology" & vbCrLf & "68b Kennels Lane" & vbCrLf & "RD2, Wanaka" & vbCrLf & "New Zealand" & vbCrLf & "Ph +64 (0)3 443 4672" & vbCrLf & "Email: will@croxford.co.nz" & vbCrLf & vbCrLf & "Regards," & vbCrLf & vbCrLf & "Will Croxford"

    .Display

End With

On Error GoTo 0

    

Set oOutlookMail = Nothing

Set oOutlookApp = Nothing

    

Application.ScreenUpdating = True

    

End Sub

Open in new window

0
Comment
Question by:willnjen
  • 3
  • 2
6 Comments
 
LVL 12

Accepted Solution

by:
Gary Dewrell earned 500 total points
ID: 33505697
Not a VBA expert but see if this at least gets you pointed in the right direction.

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

Author Closing Comment

by:willnjen
ID: 33506191
Thanks, I had already glanced over that website and didn't understand it, but after you pointed me there again I re-read it and figured it out.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 33506224
Hi, Will.

The problem is line 30.  It's wiping the signature and anything else in the message body out in favor of the content line 30 inserts.  Change the line to

    .Body = "Dear " & sContact & vbCrLf & vbCrLf & "Your website www." & sSubject & " annual " & sHosting & " is due for renewal on " & sDueDate & "." & vbCrLf & vbCrLf & "Please find attached an invoice for $" & sAmount & " for one years renewal of these services." & vbCrLf & vbCrLf & "This email is automatically generated. Please feel free to contact us should you need help. If you consider this invoice is incorrect, or you have been wrongly sent this mail, please contact us." & vbCrLf & vbCrLf & "Croxford Technology" & vbCrLf & "68b Kennels Lane" & vbCrLf & "RD2, Wanaka" & vbCrLf & "New Zealand" & vbCrLf & "Ph +64 (0)3 443 4672 begin_of_the_skype_highlighting              +64 (0)3 443 4672      end_of_the_skype_highlighting" & vbCrLf & "Email: will@croxford.co.nz" & vbCrLf & vbCrLf & "Regards," & vbCrLf & vbCrLf & "Will Croxford" & .Body

This will insert the contents from line 30 in front of the signature and/or anything else that's already in the body.
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:willnjen
ID: 33506281
FYI, I've merged rondebruin's way with what I had, to come up with this (still needs tidying)

Sub SendOutlookInvoice2()
Dim ws As Worksheet, sTo As String, sAmount As String, sDueDate As String, sSubject As String
Dim oOutlookApp As Object, oOutlookMail As Object
Dim SigString As String
Dim Signature As String
Dim strbody As String

Set oOutlookApp = CreateObject("Outlook.Application")
oOutlookApp.Session.Logon
    


Cells(5, ActiveCell.Column).Select

Application.ScreenUpdating = False
    
Set ws = ActiveSheet
    
With ws
    sTo = ActiveCell.Offset(52, 0).Value
    sAmount = ActiveCell.Offset(14, 0).Value
    sDueDate = ActiveCell.Value
    sSubject = ActiveCell.Offset(-2, 0).Value
    sContact = ActiveCell.Offset(51, 0).Value
    sHosting = ActiveCell.Offset(53, 0).Value
End With

strbody = "<SPAN>Dear </SPAN>" & sContact & "<br /><br />" & _
              "Your website www." & sSubject & " annual " & sHosting & " is due for renewal on " & sDueDate & "." & "<br /><br />" & _
              "Please find attached an invoice for " & sAmount & "0 +GST for one years renewal of these services." & "<br /><br />" & _
              "This email is automatically generated.  Please feel free to contact us should you need help.  If you consider this invoice is incorrect, or you have been wrongly sent this mail, please contact us." & "<br /><br />" & _
              "<br><br>Regards" & _
              "<br><br>Will Croxford"
              
Set oOutlookMail = oOutlookApp.CreateItem(0)

    SigString = "C:\Users\Will\AppData\Roaming\Microsoft\Signatures\Croxford Technology VCard.htm"

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


On Error Resume Next
With oOutlookMail
    .To = sTo
    .CC = ""
    .BCC = ""
    .Subject = "Annual renewal of website www." & sSubject
    .HTMLBody = strbody & "<br><br>" & Signature
    '"This email is automatically generated. Please feel free to contact us should you need help. If you consider this invoice is incorrect, or you have been wrongly sent this mail, please contact us." & vbCrLf & vbCrLf & "Croxford Technology" & vbCrLf & "68b Kennels Lane" & vbCrLf & "RD2, Wanaka" & vbCrLf & "New Zealand" & vbCrLf & "Ph +64 (0)3 443 4672" & vbCrLf & "Email: will@croxford.co.nz" & vbCrLf & vbCrLf & "Regards," & vbCrLf & vbCrLf & "Will Croxford" & vbCrLf & vbCrLf & Signature
    .Display
End With
On Error GoTo 0
    
Set oOutlookMail = Nothing
Set oOutlookApp = Nothing
    
Application.ScreenUpdating = True
    
End Sub

Open in new window

0
 

Author Comment

by:willnjen
ID: 33506465
Hi BlueDevil

You are correct and your simple solution would have saved me time, but I've already done the other HTML method to resolve my problem.  

Thanks for your suggestion anyway.

Will
0
 
LVL 76

Expert Comment

by:David Lee
ID: 33506841
You're welcome, Will.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes Outlook might have problems sending a message. There may be various causes- corrupted PST, AV scanner etc. The message, instead of going to the Sent Items folder, sits in the Outbox indefinitely. To remove it you can use a free tool cal…
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now