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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending Word Doc as Email TextBody 2

Status
Not open for further replies.

dmears1

Technical User
Jun 18, 2003
208
US
Hoping for a little help here. I am working on a script that will send automated emails. I have run into a little snag. After moving email addresses in Access into an array (emailArray), I am using the following code to send the email:

Code:
For i = 0 to UBound(emailArray)

'Sending a text email using a remote server  
Set objMessage = CreateObject("CDO.Message") 
objMessage.Subject = "Example CDO Message" 
objMessage.Sender = "me@myemail.com" 
objMessage.To = emailArray(i) 
objMessage.TextBody = "Testing"

'==This section provides the configuration information for the remote SMTP server.
'==Normally you will only change the server name or IP.
objMessage.Configuration.Fields.Item _
("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2 

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = "smtp.myserver.com"

'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25 
objMessage.Configuration.Fields.Update

'==End remote SMTP server configuration section==
objMessage.Send

Next

Here's where I'm stuck...I need the actual content of the email (currently objMessage.TextBody = "Testing") to be the contents of a Word document.

I have been unable to find any resources/tutorials online that might help. If anyone could point me the right direction it would be greatly appreciated. Thanks in advance for any help.

 
Thanks for the reply.

I am able to add the Word doc as an attachment, unfortunately the requirements of the project are that the Word doc be sent as the actual email, not as an attachment.

Thanks again for the suggestions. Any other ideas?

 
Hello dmears1!

Try something like this:

Dim objWord

Set objWord = WScript.CreateObject("Word.Application")
objWord.Documents.Open("C:\YourDocument.doc")
objMessage.TextBody = objWord.ActiveDocument.Content.Text
objWord.Quit True
set objWord = nothing

Thanks
 
Wow! Thanks Programmer 1974!
I just had to remove the WScript from the Set objWord & it worked. I spent all morning trying to do this.

If I could ask one other question: Is there a way to send the email as RTF? or would the original Word doc have to be saved as a RTF file?

Thanks again!

 
You may try something like this:
Set objWord = CreateObject("Word.Application")
objWord.Documents.Open("C:\YourDocument.doc")
objWord.DisplayAlerts = False
objWord.ActiveDocument.SaveAs "C:\YourDocument.rtf", 6
objWord.Quit
Set objWord = Nothing
objMessage.TextBody = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\YourDocument.rtf", 1).ReadAll

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks for replying PHV,

I tried your suggestion but the email was unreadable afterwards. I probably did not ask the right question.

The Word doc that I receive is formatted (such as bold, italics, etc,...). As my script stands now, the email is unformated, not even any paragraph breaks. I didn't know if there was a way to retain the formatting when inserting the Word doc into the email.

Thanks again.

 
And this ?
You may try something like this:
Set objWord = CreateObject("Word.Application")
objWord.Documents.Open("C:\YourDocument.doc")
objWord.DisplayAlerts = False
objWord.ActiveDocument.SaveAs "C:\YourDocument.txt", 5
objWord.Quit
Set objWord = Nothing
objMessage.TextBody = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\YourDocument.txt", 1).ReadAll

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
An idea if you're big in to formatting your e-mails...

You are able to send the text of your e-mail as an HTML document. For example:

objEmail.HTMLBody = "<B>This is a test</B><BR><H1>Another test</H1>"

Enjoy!
 
PHV: Thanks again for the reply. Converting the .doc to a .txt file causes it to lose formatting also.

Programmer1974: Thanks also for the reply. Good suggestion, but I'm looking for a way to have this automated so that I don't have to format the whole email myself (since this is already done in the Word doc).

I have searched for any documentation on CreateObject("Word.Application") & ActiveDocument properties but am unable to find any good references. Any links you guys know of that might be of some help?

 
Just wanted to post an update. I took Programmer1974's advice and used the objEmail.HTMLBody, along with:
Code:
emailTxt = objWord.ActiveDocument.Content.Text
emailTxt = Replace(emailTxt, Chr(13), "<br>")
This at least allowed me to format the paragraphs. We'll just have to do without bold & italic formatting.

Thanks again for all the help...

 
Just wanted to post back with a solution I've come up with. First of all I have to save the Word doc as an HTML file (ex: test.doc --> saves as --> test.htm). Then use the following code and it keeps all the formatting!


Code:
For i = 0 to UBound(emailArray) - 1

'Sending a text email using a remote server  
Set objMessage = CreateObject("CDO.Message") 
objMessage.Subject = "Testing"
objMessage.Sender = """Members"" <whatever@whatever.com>"
objMessage.To = emailArray(i)
objMessage.[b]CreateMHTMLBody "file:\\C:\...\test.htm"[/b]


This keeps all the formatting. Thanks to everyone who took the time to help!

 
Good job dmears1!

Where did you find documentation on
Code:
objMessage.CreateMHTMLBody
That's a new one for me???

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top