Solved

Excel VBA create Outlook Email format

Posted on 2010-08-23
6
1,004 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Use email signature images to promote corporate certifications and industry awards.
Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
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…

706 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

16 Experts available now in Live!

Get 1:1 Help Now