PDA

View Full Version : Sending formatted email using Excel



Domski
12-04-2009, 09:14 AM
Hi,

Having recently moved from Notes to Outlook I've been getting my hands dirty trying out some code.

What I was asked for was to develop something that would allow us to send out an email containing formatted text, images and personalised attachments to each of our 11,000+ employees.

After some research I came across some code on Sue Mosher's site (www.outlookcode.com (http://www.outlookcode.com)) that suggested the way to do this was to create the formatted email body text in a Word document and then use the code to convert that to an email and send it.

The code I have come up with is as follows:


Sub SendDocAsMsg()
' Adapted from code found on Sue Mosher's site www.outlookcode.com (http://www.outlookcode.com)
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim outApp As Outlook.Application

Dim itm As Object
Dim ID As String
Dim blnWeOpenedWord As Boolean

Dim bodyFname As String

Dim wb1 As Workbook
Dim TempFilePath As String
Dim TempFileName As String

Dim lastRow As Long, myRow As Long

Dim strPersNum As String
Dim strEmail As String
Dim strMergeName As String

On Error Resume Next

' Prompt user for data file and open
TempFileName = Application.GetOpenFilename("Select data source (*.xls), *.xls")
If TempFileName = "" Then

MsgBox "No file selected!", vbCritical
Exit Sub

End If

Application.ScreenUpdating = False

Set wb1 = Workbooks.Open(TempFileName)
TempFilePath = wb1.Path
lastRow = wb1.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

Set wrdApp = GetObject(, "Word.Application")
If wrdApp Is Nothing Then
Set wrdApp = CreateObject("Word.Application")
blnWeOpenedWord = True
End If

bodyFname = ThisWorkbook.Path & "\Body Text.doc"

For myRow = 2 To lastRow

Application.StatusBar = "Processing record " & myRow - 1 & " of " & lastRow - 1
' Set personnel number, name and email details
strPersNum = wb1.Sheets("Sheet1").Cells(myRow, 1).Value
strEmail = wb1.Sheets("Sheet1").Cells(myRow, 2).Value
strMergeName = wb1.Sheets("Sheet1").Cells(myRow, 12).Value

Set wrdDoc = wrdApp.Documents.Open _
(Filename:=bodyFname, ReadOnly:=True)
Set itm = wrdDoc.MailEnvelope.Item
With itm
.To = strEmail
.Subject = "Metro card renewal"
.ReplyRecipients.Add "joe.bloggs@leeds.gov.uk"
.Attachments.Add TempFilePath & "\letters\" & strMergeName & ".doc"
.Attachments.Add TempFilePath & "\Additional Info.doc"
.Send
ID = .EntryID
End With
Set itm = Nothing

Set itm = outApp.Session.GetItemFromID(ID)
itm.Send
wrdDoc.Close False
If blnWeOpenedWord Then
wrdApp.Quit
End If
Next myRow

Set wrdDoc = Nothing
Set itm = Nothing
Set wrdApp = Nothing

wb1.Close

Application.StatusBar = False
Application.ScreenUpdating = True

End Sub

The main problem is that it is very slow and takes around a minute to send 10 emails. Obviously with the volume I am planning to send the code is going to have to run for quite a long time.

Has anyone else got suggestions as to a better way to achieve this sort of thing or how the code could be made more efficient.

Any help appreciated.

Dom

ajetrumpet
12-05-2009, 09:15 PM
check this link out:

access-programmers.co.uk/forums/showthread.php?t=181932

it is for access, but the same holds true for any microsoft program you are trying to send email through.


that method sends emails through outlook right away, but the best way to do it is to pile them up in the outbox, then push the send button manually so they don't pile up on you and get confused when the code is running. the code I posted there will pile 11,000 emails in the outbox in probably 20 seconds or less (I have not tested that many, but that's a guess of course)

and another useful link here: access-programmers.co.uk/forums/showthread.php?t=182015

Domski
12-07-2009, 03:10 AM
Thanks for the reply.

The issue isn't the the sending of the emails or the volume which I could do using Outlook or CDO but the issue of the email body containing formatted text, images.

I think the bottleneck with this is the fact that for each email that is sent it is having to convert the Word doc to an email...add attachements and then send the email.

I was just wondering if anyone had a better approach.

Thanks,

Dom

Domski
12-09-2009, 07:32 AM
Also posted here: http://www.mrexcel.com/forum/showthread.php?p=2147692#post2147692

Dom