How can I use VBA to print only the body of an email in Office 2007?

Posted on 2009-12-28
Last Modified: 2013-11-10
I need to automate a process to recieve certain emails based on the sender and print to a thermal POS printer. However, the print driver is doing a lousy job with the margins of the outlook emails, so I would like to just print the body directly as a string without the header information( To:, From:, Subject: etc.)My email is hosted by gmail, so I am also open to a process that will skip outlook entirely, but I imagine that to be a lot more complicated.

Below is the simple script I have so far, but does not work because the "PrintOut" function cannot be used with a string. Please forgive me if this is extremely simple, I do not have a great knowledge of programming.
Sub PrintBody(Msg As Outlook.MailItem)

Dim Subj As String, Body As String

Subj = Msg.Subject

Body = Msg.HTMLBody


End Sub

Open in new window

Question by:SentientAlex
    LVL 38

    Accepted Solution

    Here's a way to do it by creating a text file and then printing it with Notepad.

    Customize the value of the strTempDir variable on line 2 with the location of a folder to which users have Write permission.  Do not include a trailing backslash.

    Sub PrintBody(Msg As Outlook.MailItem)
        strTempDir = "c:"
        strFileName = "emailbody.txt"
        strBody = Msg.Body
        strTempFile = strTempDir & "\" & strFileName
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objTempFile = objFSO.CreateTextFile(strTempFile, True)
        objTempFile.Write strBody
        Set objShell = CreateObject("Shell.Application")
        Set objFolder = objShell.Namespace(strTempDir) 
        Set objFolderItem = objFolder.ParseName(strFileName)
    End Sub

    Open in new window


    Author Closing Comment

    Thank you, this works great.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    improve Shell script 5 115
    lucky13 challenge 11 94
    copyEndy  challenge 15 37
    Exe program is not a valid Win 32 application 15 71
    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    A short article about problems I had with the new location API and permissions in Marshmallow
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now