Solved

Email template with variation in subject and attachment

Posted on 2011-03-10
7
142 Views
Last Modified: 2013-11-05
I need to send some emails everyday with almost the same format:
To: someone1@gmail.com
CC: someone2@gmail.com, someone3@gmail.com

Content:
"some text..."

The only difference are:
Subject: Company Report 03-11
Attachment: 2011-03-11 Company Report.xls

Both of the dates will be today's date, and the excel file is already in the same folder "H:\Reports" with correct name

How can I use some VBA to do this automatically? I don't need to send it right away, have to edit something before sending.
0
Comment
Question by:mmcompact
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 35099145
Hi, mmcompact.

This should do it.
Sub SendCannedMessage()
    Dim olkMsg As Outlook.MailItem, strFilename As String
    strFilename = Format(Date, "yyyy-mm-dd") & " Company Reports.xls"
    Set olkMsg = Application.CreateItem(olMailItem)
    With olkMsg
        .To = "someone1@gmail.com"
        .CC = "some2@gmail.com; someone3@gmail.com"
        .HTMLBody = "Some text ..."
        .Subject = "Company Report " & Format(Date, "mm-dd")
        .Attachments.Add "H:\Reports\" & strFilename
        .Recipients.ResolveAll
        .Display
    End With
    Set olkMsg = Nothing
End Sub

Open in new window

0
 

Author Comment

by:mmcompact
ID: 35099662
BlueDevilFan:

Thanks. The HTMLbody is actually a quite long html file. Can I still copy and paste the content there or I need to link to the actual file?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 35100039
I can add code that reads in from the file or you can copy and paste.  You're choice.
0
 

Author Comment

by:mmcompact
ID: 35100404
Read from file will be better. thanks
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 35100659
Try this.
Sub SendCannedMessage()
    Dim olkMsg As Outlook.MailItem, strFilename As String, objFSO as Object, objFil As Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Change the file name on the next line'
    Set objFil = objFSO.OpenTextFile("C:\SomeFileName.htm")
    strFilename = Format(Date, "yyyy-mm-dd") & " Company Reports.xls"
    Set olkMsg = Application.CreateItem(olMailItem)
    With olkMsg
        .To = "someone1@gmail.com"
        .CC = "some2@gmail.com; someone3@gmail.com"
        .HTMLBody = objFil.ReadAll
        .Subject = "Company Report " & Format(Date, "mm-dd")
        .Attachments.Add "H:\Reports\" & strFilename
        .Recipients.ResolveAll
        .Display
    End With
    objFil.Close
    Set objFil = Nothing
    Set objFSO = Nothing
    Set olkMsg = Nothing
End Sub

Open in new window

0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Mailbox Overload?
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

617 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