[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2009-12-28
Medium Priority
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

Shift-3 earned 1000 total points
ID: 26133924
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

ID: 31670528
Thank you, this works great.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Simple Linear Regression

834 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