Results 1 to 5 of 5
  1. #1
    TheDeceived is offline Novice
    Windows Vista Access 2007
    Join Date
    Sep 2010
    Posts
    8

    Email report as body of email (RTF)

    Hi all,

    Having spent the last hour Googling sites for answers to the issue I'm having, I haven't been able to find a solution so apologies for creating another thread about this (it seems this is quite a popular problem people are having!).

    So basically, I want to email a report I'm generating in Access to someone but putting the report into the body of the email, not sending it as an attachment. After looking through these forums, I found the following thread which was really useful on getting the basics done:

    https://www.accessforums.net/program...ody-10101.html



    This is the code I've skilfully copied and pasted into my Access db:

    Code:
    Function sendRTFEmail()
    
        Const ForReading = 1, ForWriting = 2, ForAppending = 3
        
        Dim fs, f
        Dim objOL As Outlook.Application
        Dim MyItem As Outlook.MailItem
        
        Set objOL = New Outlook.Application
        Set MyItem = Outlook.Application.CreateItem(olMailItem)
        
        DoCmd.OutputTo acOutputReport, "SignOff", acFormatRTF, "C:\Users\john_smith\Desktop\ExampleRTFReport.rtf"
        
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set f = fs.OpenTextFile("C:\Users\john_smith\Desktop\ExampleRTFReport.rtf", ForReading)
        RTFBody = f.readall
        f.Close
        
        With MyItem
            .To = "john.smith@example.com"
            .Subject = "Sign Off email"
            .body = RTFBody
        End With
        
        MyItem.display
    End Function
    So the above code correctly creates an email and populates the "To" and "Subject" fields fine. However, when it goes to populate the body of the email, this is what I get:

    http://i47.tinypic.com/2wcenb5.jpg

    It's not replicating the layout of the report in the body of the email. It just seems to be giving me a load of rubbish instead!

    I've also tried changing the ".body" to ".HTMLBody" but that doesn't seem to help.

    Does anyone have any idea where I'm going wrong?

    Thanks!

  2. #2
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    I know what the problem is, but I'm not sure how to fix it.

    What's happening is that you are reading the rtf file as a text file, and OpenTextFile just gives you whatever the file contains, as plain text - it has no idea how to decode the rtf formatting, and Outlook doen't know its getting rtf either.

    I'm not sure if you can do what you want to do - my guess is no, bul I'll leave that to the real gurus. I think you would be better off just using an attachment.

    HTH

    John

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I suspect John is right. An untested option would be to export the report in HTML format instead of RTF, and obviously use HTMLBody. That may not work if the report is longer than a page. If memory serves, Access creates a separate HTML file for each page.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Do you have Word? You could open the file in Word and then 'Save As' a text file to be used in your email. It should be possible to do this automatically and behind the scenes. I have no idea what happens to the indenting, line spacing, etc.

  5. #5
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    You need to set .BodyFormat to olFormatRichText

    Code:
    Function sendRTFEmail()
        Const ForReading = 1, ForWriting = 2, ForAppending = 3
    
        Dim fs, f
        Dim objOL As Outlook.Application
        Dim MyItem As Outlook.MailItem
    
        Set objOL = New Outlook.Application
        Set MyItem = Outlook.Application.CreateItem(olMailItem)
    
        DoCmd.OutputTo acOutputReport, "SignOff", acFormatRTF, "C:\Users\john_smith\Desktop\ExampleRTFReport.rtf"
    
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set f = fs.OpenTextFile("C:\Users\john_smith\Desktop\ExampleRTFReport.rtf", ForReading)
        RTFBody = f.readall
        f.Close
    
        With MyItem
            .To = "john.smith@example.com"
            .Subject = "Sign Off email"
            .body = RTFBody
            .BodyFormat = olFormatRichText
        End With
    
        MyItem.display
    End Function

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Email report as body of email
    By chrish20202 in forum Programming
    Replies: 6
    Last Post: 01-15-2012, 07:23 PM
  2. Replies: 1
    Last Post: 05-23-2011, 08:07 AM
  3. Email from report to Email body
    By Ehmke66 in forum Programming
    Replies: 4
    Last Post: 01-03-2011, 01:06 PM
  4. Export Contents of a Report into Email Body
    By Nosaj08 in forum Reports
    Replies: 4
    Last Post: 05-27-2009, 09:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums