VBA printing from Visual Basic 6 (SP6)

Hi Experts,

I'm having trouble setting the printer for Word in VB 6. If I were to print an Excel sheet, I'll do:

Dim excel_app As Excel.Application


    excel_app.ActiveWorkbook.PrintOut Copies:=prnjob.numPages, ActivePrinter:=prnjob.strPrinter
    excel_app.Wait (Now + TimeValue("0:00:3")) ' Wait

were prnjob is a simple UDT that stores users print preferences, but:

Since here's no ActivePrinter property for word_app.PrintOut I've tried:

Dim wordApp As Word.Application
Dim wordDoc As Word.Document


   ' Try to set default printer
    Dim prtDev As Object
    For Each prtDev In Printers
      If prtDev.DeviceName = prnjob.strPrinter Then
        Set Printer = prtDev
        Exit For
      End If
    Next prtDev
    word_app.PrintOut Copies:=prnjob.numPages  

I've to say that I don't have a printer attached to the PC (it's broken), instead I use Adobe distiller and Microsoft Fax for testing. This works perfectly in excel, but word always prints to it's default printer (Adobe).

Also, there's no wait procedure for word objects (not that I'm happy with the excel code, but that works). So How do I make sure printing is
finnished before I destroy the object?

Last, but not least. My process list still shows Word (and sometime excel) even that I do:

     excel_app.ActiveWorkbook.Close False ' Close the workbook without saving. (If true will ask for filename)

  wordApp.Quit SaveChanges:=False
  Set wordDoc = Nothing ' wordDoc = wordApp.ActiveDocument
  Set wordApp = Nothing

Why this doesn't kill them?

I'll be happy with docs/links and I would really appreciate some working code.

Thank you

Who is Participating?
This routine is what I use... there's a bit of extra code in it (ShowPrinters subroutine) that you can remove if you want.

Const wdDoNotSaveChanges = 0

'sDefault = ShowPrinters

sDocument = "C:\something.txt"

' if you want to have the document name as a command line
' input parameter, enable the two lines below

Set oArg = WScript.Arguments
sDocument = oArg.Item(0)

' printer that is to be used
sPrinter = "Local Panasonic"      ' <== This will be one of the oItem.Name values from ShowPrinters

With CreateObject("Word.Application")
  ' if you want to hide Word, set .Visible to False
  .Visible = True
  .Options.PrintBackground = False

  ' get current active printer
  sActivePrinter = .Application.ActivePrinter

  ' set active printer
  .Application.ActivePrinter = sPrinter

  ' print the document

  ' set current active printer back to original
  .Application.ActivePrinter = sActivePrinter

  ' quit without saving any changes
  .Application.Quit wdDoNotSaveChanges
End With

Sub ShowPrinters
sComputer = "."
Set oWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set colItems = oWMIService.ExecQuery("Select * from Win32_Printer",,48)
For Each oItem in colItems
   If (oItem.Attributes And 2^(3-1)) = 4 Then
      sDefault = oItem.Name
      wscript.echo oItem.Name & "  <== default."
      wscript.echo oItem.Name
   End If
End Sub

Tommy KinardCommented:
Hi benjami,

Sorry but I don't know a way around the default printer. I use WordDoc.PrintOut (False)  to print the doc which will not go past the statement untill Word is through Printing.

WordDoc.Close SaveChanges:=wdDoNotSaveChanges  'tell the doc to close, not the app
wordApp.Quit   'tell the app to quit

Then set them to nothing


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.