Consulting

Results 1 to 2 of 2

Thread: Solved: Sending text to Printer

  1. #1

    Solved: Sending text to Printer

    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?

  2. #2
    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:

    [VBA]
    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
    [/VBA]

Posting Permissions

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