Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I copy an entire Word document into an Outlook email?

Status
Not open for further replies.

jhamlett

Programmer
Oct 8, 2003
2
US
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.
 
Why not sending the doc as an Attachment instead ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Your olMailItem may be plain text.

Try setting the body format to HTML

objEMail.BodyFormat = olFormatHTML
 
It still will not use the document as it is formatted in Word.

Gerry
 
Thank you guys (and gals, if there are any) for your tips. Sending as an attachment is the best solution; however, not all persons can accept email with attachments (security on their server, etc.). I've searched numerous sites for an answer and have come close. But so far I've been unable to find a solution or a person with the solution. I guess I'll just keep searching.

I don't know how to react to PH's comments. I did check out those two articles (2884 & 2886) before posting my question. If you can help me understand what I missed, I'd appreciate it. I don't want to waste anyone's time. I understand the value.

Again, folks, thanks for your suggestions.

Jim H.


 



I don't know how to react to PH's comments.

Its boilerplate in PHV's signature on each of his posts.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Can you use
expression.SaveAs(FileName, FileFormat) using html
after the file selection
Open the new file
Copy the contents and
Use that to paste into the email body?

This old world keeps spinning round - It's a wonder tall trees ain't layin' down
 
white605, not if you are using Word.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top