I'm trying to send out a couple hundred emails from a recordset in an Access table using the content of a Word document (includes text and a .jpg file). Everything works except my method of trying to copy the document. I get the text, but not the graphic, and my text loses its formatting (i.e., italics, bolding, etc). How do I properly refer to the entire contents (wholestory?) of a Word doc so that when I set the .Body of the email to that object, I get exactly what's in the Word document? Here's some of my code:
Private Sub cmdSendEMail_Click()
On Error GoTo Err_cmdSendEMail_Click
Dim db As Database
Dim rs As Recordset
Dim objOutlook As New Outlook.Application
Dim objEMail As MailItem
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objItem As Object
Dim blnOpenedWord As Boolean
Dim strPathFile As String
Dim strFilename As String
Dim strMsg As String
Dim intMsg As Integer
'Prompt user for file to use for the email body; give opportunity to exit.
strMsg = "Click OK to select your document to use for this email." & vbCrLf & _
" Click Cancel to stop this email process."
intMsg = MsgBox(strMsg, vbOKCancel, "Select File")
If intMsg = vbCancel Then
Exit Sub
End If
'varFileName = tsGetFileFromUser()
strFilename = CStr(tsGetFileFromUser())
Set db = CurrentDb
Set rs = db.OpenRecordset("qselEmailAddresses")
Set objWord = GetObject(, "Word.Application")
If objWord Is Nothing Then
Set objWord = CreateObject("Word.Application")
blnOpenedWord = True
End If
Set objDoc = objWord.Documents.Open _
(FileName:=strFilename, ReadOnly:=True)
'Determine number of email addresses and prompt user to continue.
If rs.RecordCount = 0 Then
MsgBox "No email addresses were found.", vbOKOnly, "No Addresses"
Exit Sub
Else
intMsg = MsgBox("You are about to send " & rs.RecordCount & " email messages." & vbCrLf & _
" Press OK to continue.", vbOKCancel, "Send Emails")
If intMsg = vbCancel Then
Exit Sub
End If
End If
'If user continues, scroll through records and send separate email per addresses found.
rs.MoveFirst
Do Until rs.EOF()
'Create email object, fill fields and send for each email address in recordset.
Set objEMail = objOutlook.CreateItem(olMailItem)
With objEMail
.To = rs!email1
If Not IsNull(Me.txtEmailSubject) Then
.Subject = Me.txtEmailSubject
End If
.Body = objDoc.Content.FormattedText
If Not IsNull(Me.txtEmailAttach) Then
strPathFile = Me.txtEmailAttach
.Attachments.Add strPathFile
End If
.Send
End With
rs.MoveNext
Loop
objDoc.Close wdDoNotSaveChanges
If blnOpenedWord Then
objWord.Quit
End If
objOutlook.Quit
Set objEMail = Nothing
Set objOutlook = Nothing
Set objWord = Nothing
Set objDoc = Nothing
MsgBox rs.RecordCount & " emails have been sent.", vbOKOnly, "Emails Sent"
Exit_cmdSendEMail_Click:
Exit Sub
Err_cmdSendEMail_Click:
MsgBox Err.Description
Resume Exit_cmdSendEMail_Click
End Sub
Many thanks for any help.
Jim H.
Private Sub cmdSendEMail_Click()
On Error GoTo Err_cmdSendEMail_Click
Dim db As Database
Dim rs As Recordset
Dim objOutlook As New Outlook.Application
Dim objEMail As MailItem
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objItem As Object
Dim blnOpenedWord As Boolean
Dim strPathFile As String
Dim strFilename As String
Dim strMsg As String
Dim intMsg As Integer
'Prompt user for file to use for the email body; give opportunity to exit.
strMsg = "Click OK to select your document to use for this email." & vbCrLf & _
" Click Cancel to stop this email process."
intMsg = MsgBox(strMsg, vbOKCancel, "Select File")
If intMsg = vbCancel Then
Exit Sub
End If
'varFileName = tsGetFileFromUser()
strFilename = CStr(tsGetFileFromUser())
Set db = CurrentDb
Set rs = db.OpenRecordset("qselEmailAddresses")
Set objWord = GetObject(, "Word.Application")
If objWord Is Nothing Then
Set objWord = CreateObject("Word.Application")
blnOpenedWord = True
End If
Set objDoc = objWord.Documents.Open _
(FileName:=strFilename, ReadOnly:=True)
'Determine number of email addresses and prompt user to continue.
If rs.RecordCount = 0 Then
MsgBox "No email addresses were found.", vbOKOnly, "No Addresses"
Exit Sub
Else
intMsg = MsgBox("You are about to send " & rs.RecordCount & " email messages." & vbCrLf & _
" Press OK to continue.", vbOKCancel, "Send Emails")
If intMsg = vbCancel Then
Exit Sub
End If
End If
'If user continues, scroll through records and send separate email per addresses found.
rs.MoveFirst
Do Until rs.EOF()
'Create email object, fill fields and send for each email address in recordset.
Set objEMail = objOutlook.CreateItem(olMailItem)
With objEMail
.To = rs!email1
If Not IsNull(Me.txtEmailSubject) Then
.Subject = Me.txtEmailSubject
End If
.Body = objDoc.Content.FormattedText
If Not IsNull(Me.txtEmailAttach) Then
strPathFile = Me.txtEmailAttach
.Attachments.Add strPathFile
End If
.Send
End With
rs.MoveNext
Loop
objDoc.Close wdDoNotSaveChanges
If blnOpenedWord Then
objWord.Quit
End If
objOutlook.Quit
Set objEMail = Nothing
Set objOutlook = Nothing
Set objWord = Nothing
Set objDoc = Nothing
MsgBox rs.RecordCount & " emails have been sent.", vbOKOnly, "Emails Sent"
Exit_cmdSendEMail_Click:
Exit Sub
Err_cmdSendEMail_Click:
MsgBox Err.Description
Resume Exit_cmdSendEMail_Click
End Sub
Many thanks for any help.
Jim H.