View Full Version : Solved: Sending text to Printer
chocobochick
09-14-2005, 01:43 PM
Is there a way to just send text (not a document) to a printer in VBA? For instance, I'm writing a macro for Outlook to automatically print each email (and all its attachments), but if I come across an unrecognized extension in the attachments, I want to print error data in its place. How do I go about achieving this?
chocobochick
09-21-2005, 09:13 AM
After a bit of research, I've concluded that there might be a way, but it isn't easy. VBA, unlike regular VB, does not appear to have any access to the Printers collection or Printer objects -- or at least not in Office 2000. So I devised a function to use MS Word's PrintOut command for use with any string argument, like this:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
' Sends string to default printer
Public Sub PrintText(ByVal s As String)
On Error GoTo PrintTextError
Dim wrdApp As Object ' MS Word Application
Dim wrdDoc As Object ' MS Word Document
Dim wrdRange As Object ' MS Word Range
' Write temporary document
Set wrdApp = CreateObject("Word.Application")
Set wrdDoc = wrdApp.Documents.Add
Set wrdRange = wrdDoc.Range()
wrdRange.InsertBefore s
wrdDoc.PrintOut
' Let procedure sleep to prevent Word from closing before doc is sent to printer.
Sleep 500 + 2 * Len(s)
wrdApp.Quit 0
PrintTextError:
End Sub
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.