PDA

View Full Version : Word doc in email body



Ooogy
06-20-2007, 07:39 AM
I'm attempting to insert a Word document into the body of an email created by the Outlook mail object in an Excel worksheet. Essentially, the user clicks a name in the workbook and an email pops up with the contents of a shared Word doc as the body.

Having all sorts of problems with the mail object reading carriage returns as EOF markers, as well, it can't handle the bulleted list the document's author really wants to be included. I have experimented (successfully) with removing the carriage returns, replacing them with "~" in the original txt file, and then replacing the "~"'s with vbcrlf's once the object is fully read and into a string variable. Unfortunately, the formatting from the original text is all but gone using this method. Not good.

Can someone tell me in general terms what tree I should be barking up to to get this accomplished?

Here's what I'm using so far..


Dim sText As String
Dim sFile As String
Dim iFileNum As Integer
sFile = "M:\Reenroll_Tracking_Email.txt"
iFileNum = FreeFile
Open sFile For Input As iFileNum
Input #iFileNum, sText
Close #iFileNum

Dim oOApp As outlook.Application
Dim oOMail As outlook.MailItem
Set oOApp = CreateObject("Outlook.Application")
Set oOMail = oOApp.CreateItem(olMailItem)
With oOMail
.To = ActiveCell.Value
.Subject = "Email subject"
.Body = sText
.Display
End With

Set oOApp = Nothing
Set oOMail = Nothing


Thanks for any help at all!...Ooogy :banghead:

mvidas
06-20-2007, 08:25 AM
This part is more of an FYI than anything, but there is an easier way to get all the contents of a text file into one variable:'iFileNum = FreeFile
'Open sFile For Input As iFileNum
'Input #iFileNum, sText
'Close #iFileNum
iFileNum = FreeFile
Open sFile For Binary Access Read As #iFileNum
sText = Space$(LOF(iFileNum))
Get #iFileNum, , sText
Close #iFileNum

However, if you're using a word doc and want HTML formatting, use .HTMLBody instead of .Body for the email. For a word doc you wouldn't use the above method, that is really for pure text.

Lastly, based on the .txt extension, are you sure the bullets/formatting/etc are saved into the .txt file?