VBA printing from Visual Basic 6 (SP6)

Posted on 2005-05-05
Last Modified: 2013-11-25
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

Question by:benjami
    LVL 14

    Assisted Solution

    by:Tommy Kinard
    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


    LVL 20

    Accepted Solution

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

    737 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

    19 Experts available now in Live!

    Get 1:1 Help Now