Consulting

Results 1 to 2 of 2

Thread: Word doc in email body

  1. #1
    VBAX Newbie
    Joined
    Jun 2007
    Posts
    1
    Location

    Word doc in email body

    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..

    [vba]
    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

    [/vba]

    Thanks for any help at all!...Ooogy

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    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:[vba]'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[/vba]

    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?
    Matt

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •