Printer Object and Printers Collection

Since it's part of the VB library, the Printer object isn't available to VBA applications. When you write a VBA (as opposed to VB) program, you simply make use of the host application's own built-in printing functionality. For example:

Set oWordActiveDoc = oWord.Documents.Add
Set oWordSel = oWord.Selection
            
oWordSel.TypeText "This is text coming in from the VB app."
oWordSel.WholeStory
oWordSel.Font.Name = "Arial"
oWordSel.Font.Size = 12
oWordSel.Font.Bold = wdToggle

oWordActiveDoc.PrintOut

Printer Object

  • Visual Basic contains a global printer object, which refers to the default printer for the current system. Because this object is global to all parts of the VB project, you don't need to create an object variable; you can simply use the Printer object directly. For example:

    If Printer.Duplex Then

  • The global printer object is also a virtual document onto which you place your text output using the Print method and a range of built-in graphics methods. For example:

    Printer.Print "Private and Confidential"
    Printer.Line (0, 100)-(Printer.ScaleWidth, 100)

  • You can position text and drawings precisely in output by using the Printer object's CurrentX and CurrentY properties. The coordinate system of the printer object is based from the top left corner of the object. The default coordinate unit is a Twip, but this can be changed by using the ScaleHeight, ScaleWidth, ScaleLeft, ScaleTop, and ScaleMode properties.

  • Because the output of proportional fonts is so hard to predict, two very useful properties are TextWidth and TextHeight. These return the actual size that a given string will occupy when placed on the printer object using the current font, font size, and font style. This allows you to determine at what coordinates to start printing the string on the printer object.

  • The Printer object holds a virtual document that can contain any number of pages; you start with page one, can add pages by calling the NewPage method, and can keep count of the number of pages with the Page property. For example:

    Printer.NewPage
    Printer.Print "Page No:" & Printer.Page

  • When you've finished the document and want to output it to the printer device, simply call the EndDoc method, which dumps the current Printer buffer to the device and resets the Printer object:

    Printer.EndDoc

  • A useful tip is that the PictureBox control and the Printer object have identical graphics and print methods. Why is this significant? The following code shows how you can start to create a Print Preview form using the same code to reference either a Printer object or a PictureBox control:

    Private Sub mnuPrintPreview_Click()
    
        Dim oPic As PictureBox
        Set oPic = Picture1
        If PrintOutput(oPic) Then
            oPic.Visible = True
        End If
        
    End Sub
    
    Private Sub mnuPrint_Click()
        If PrintOutput(Printer) Then
            Printer.EndDoc
        Else
            Printer.KillDoc
        End If
    End Sub
    
    Public Function PrintOutput(oPrintSurface As Object) _
                                As Boolean
        oPrintSurface.Print "Hello World"
        oPrintSurface.Line (100, 300) - (600, 300)
        PrintOutput = True
    End Function

Printer Object Properties

The Printer object supports the following properties and methods. Note that not all properties are available to all printer devices; some properties depend on the capabilities of the current device driver. In the following synopses, oPrinter is an expression that evaluates to a Printer object:


ColorMode

Returns or sets a flag denoting whether a color printer outputs in monochrome or color.

Syntax: oPrinter.ColorMode [= constValue]

where constValue can be vbPRCMMonochrome or vbPRCMColor.


Copies

Returns or sets the number of copies to be printed.

Syntax: oPrinter.Copies [=intValue]


CurrentX

Returns or sets the horizontal coordinate for the next print or drawing method.

Syntax: oPrinter.CurrentX [= singValue]


CurrentY

Returns or sets the vertical coordinate for the next print or drawing method.

Syntax: oPrinter.CurrentY [= singValue]


DeviceName

Read-only property that returns the name of the current printer device.


DrawMode

Returns or sets a flag denoting the method used to output graphics methods.

Syntax: oPrinter.DrawMode [= constValue]

constValue can be:

vbBlackness vbCopyPen (default) vbInvert
vbMaskPen vbMaskPenNot vbMaskNotPen
vbMergeNotPen vbMergePen vbMergePenNot
vbNop vbNotCopyPen vbNotMaskPen
vbNotMergePen vbNotXorPen vbXorPen
vbWhiteness  


DrawStyle

Returns or sets a flag denoting the style of lines output in graphics methods.

Syntax: oPrinter.DrawStyle [= constValue]

constValue can be vbSolid, vbDash, vbDot, vbDashDot, vbDashDotDot, vbInvisible, vbInvisibleSolid.


DrawWidth

Returns or sets the width in pixels of lines output by graphics methods.

Syntax: oPrinter.DrawWidth [= intValue]


DriverName

Returns the name of the current driver for the current Printer object.

Syntax: oPrinter.DriverName


Duplex

Returns or sets a flag denoting whether a page is to be printed on both sides.

Syntax: oPrinter.Duplex [= constValue]

constValue can be vbPRDPSimplex, vbPRDPHorizontal, vbPRDP-Vertical.


FillColor

Returns or sets the fill color used in graphics methods

Syntax: oPrinter.FillColor [= Value]

Value can be the result of a call to the QBColor function or one of the intrinsic Visual Basic color constants.


FillStyle

Returns or sets the pattern used to fill shapes created with graphics methods.

Syntax: oPrinter.FillStyle [= constValue]

constValue can be:

vbCross vbDiagonalCross vbDownwardDiagonal
vbFSSolid vbFSTransparent bHorizontalLine
vbUpwardDiagonal vbVerticalLine 


Font

Returns a Font object. This is the recommended method of setting font properties for the Printer object.

Syntax: oPrinter.Font.FontProperty [= FontPropertyValue]


FontBold, FontItalic, FontStrikeThru, FontTransparant, FontUnderline

Returns or sets the style of the current font. But it's instead recommended that you use the properties of the Font object; for example, Font.Bold.

Syntax: oPrinter.FontBold [= booleanValue]

Syntax: oPrinter.FontItalic [= booleanValue]

Syntax: oPrinter.FontStrikethru [= booleanValue]

Syntax: oPrinter.FontTransparant[= booleanValue]

Syntax: oPrinter.FontUnderline [= booleanValue]


FontCount

Returns the number of fonts available to the current Printer object.

Syntax: NoOfFonts = oPrinter.FontCount


FontName

Returns or sets the name of the current font. But to retrieve the name of the current font, you should use the Font.Name method instead.

Syntax: oPrinter.FontName[= strVal]


Fonts

Returns a collection of all fonts available to the current Printer object. An individual font can be accessed by its ordinal position in the collection.

Syntax: oPrinter.Fonts(index)


FontSize

Returns or sets the font size in points of the current font. The recommended method is to use the Font.Size property instead.

Syntax: oPrinter.FontSize [= sngVal]


hDC

Returns a window handle that can be used in a Win32 API call to identify the current Printer object. Don't store hDC property values, since they can change while the application is executing.

Syntax: oPrinter.hDC


Height

Returns or sets the height of the current printer object in twips. Setting the height property overrides the current PaperSize property.

Syntax: oPrinter.Height [= sngVal]


Orientation

Returns or sets the orientation of the printed output.

Syntax: oPrinter.Orientation [= constValue]

constValue can have these settings: vbPRORPortrait, vbPRORLandscape.


Page

Returns the number of the current page. Reset to one when the EndDoc or KillDoc methods is called. Incremented when the NewPage method is called or when textual output on the current page overruns onto subsequent pages. Note that graphical output is truncated and never overruns onto subsequent pages.

Syntax: oPrinter.Page


PaperBin

Sets or returns the tray from which paper is fed.

Syntax: oPrinter.PaperBin [= constValue]

constValue can have these settings:

vbPRBNUpper vbPRBNLower vbPRBNMiddle
vbPRBNManual vbPRBNEnvManual vbPRBNAuto
vbPRBNTractor vbPRBNSmallFmt vbPRBNLargeFmt
vbPRBNLargeCapacity vbPRBN-Cassette 


PaperSize

Sets or returns the paper size.

Syntax: oPrinter.PaperSize [= constValue]

constValue can have these settings:

vbPRPS10x14 vbPRPS11x17 vbPRPSA3
vbPRPSA4 vbPRPSA4Small vbPRPSA5
vbPRPSB4 vbPRPSB5 vbPRPSCSheet
vbPRPSDSheet vbPRPSEnv9 vbPRPSEnv11
vbPRPSEnv14 vbPRPSEnvB4 vbPRPSEnvB5
vbPRPSEnvB6 vbPRPSEnvC3 vbPRPSEnvC4
vbPRPSEnvC5 vbPRPSEnvC6 vbPRPSEnvC65
bPRPSEnv10 vbPRPSEnv12 vbPRPSEnvDL
vbPRPSEnvItaly vbPRPSEnvMonarch vbPRPSESheet
vbPRPSExecutive vbPRPSFanfoldLgl-German vbPRPSFanfoldStd-German
vbPRPSFanfoldUS vbPRPSFolio vbPRPSLedger
vbPRPSLegal vbPRPSLetter vbPRPSLetterSmall
vbPRPSNote vbPRPSStatement vbPRPSTabloid
vbPRPSQuarto vbPRPSUser 


Port

Returns the name of the port used by the current printer object.

Syntax: oPrinter.Port


PrintQuality

Returns or sets the flag denoting the printer's print quality setting.

Syntax: oPrinter.PaperSize [= constValue]

constValue can have these settings: vbPRPQDraft, vbPRPQLow, vbPRPQMedium, vbPRPQHigh.


RightToLeft (VB6 onward)

Returns a True or False value denoting whether or not text in the current system should appear from right to left, as for example when running Arabic or Hebrew Windows.

Syntax: oPrinter.RightToLeft


ScaleHeight, ScaleWidth

Returns or sets the height and width of the print object's print area based on the units defined by ScaleMode.

Syntax: oPrinter.ScaleHeight[= sngVal]

Syntax: oPrinter.ScaleWidth [= sngVal]


ScaleLeft, ScaleTop

Returns or sets the virtual top and left of the print object's print area based on the units defined by ScaleMode.

Syntax: oPrinter.ScaleLeft[= sngVal]

Syntax: oPrinter.ScaleTop [= sngVal]


ScaleMode

Determines the coordinate system used by the ScaleLeft, ScaleTop, ScaleWidth, and ScaleHeight properties. Note: due to a bug in the initial release of VB5, the ScaleMode property had no effect; this was corrected with Service Pack 2.

Syntax: oPrinter.ScaleMode [= constValue]

constValue can have these settings: vbUser, VbTwips, VbPoints, vbCharacters, VbInches, VbMillimeters, VbCentimeters.


TrackDefault

Returns or sets a Boolean flag to denote whether the Printer object should refer to the default printer, even when the default printer is changed through Control Panel (True), or whether the Printer object should continue to refer to the same printer, regardless of the system default printer setting (False).

Syntax: oPrinter.TrackDefault [= booleanValue]


TwipsPerPixelX, TwipsPerPixelY

Returns the number of twips per pixel in the horizontal plane (TwipsPerPixelX) or vertical plane (TwipsPerPixelY) of the Printer object.

Syntax: oPrinter.TwipsPerPixelX

Syntax: oPrinter.TwipsPerPixelY


Width

Returns or sets the width in twips of the current Printer object. Setting the width property overrides the current PaperSize property.

Syntax: oPrinter.Width[= sngVal]


Zoom

Returns or sets the percentage by which the printer output is scaled up or down.

Syntax: oPrinter.Zoom [= sngVal]

Printer Object Methods

In the following synopses, oPrinter is an expression that evaluates to a Printer object:


EndDoc

Outputs the Printer object's output buffer to the printer driver or spooler.

Syntax: oPrinter.EndDoc


KillDoc

Terminates the current job, emptying the Printer object's output buffer and, if possible, deleting the current print job.

Syntax: oPrinter.KillDoc


Line

Outputs a line, a box outline, or a filled box.

Syntax: oPrinter.Line [Step] (x1, 1) [Step] (x2, y2), [color], [B][F]


NewPage

Adds a page to the Printer object and increments the Page property by 1. CurrentX and CurrentY are set to 0,0 on the new page.

Syntax: oPrinter.NewPage


PaintPicture

Introduced with VB5, the method is a wrapper for Win32 API BitBlt functions to allow .bmp, .wmf,.emf, .ico, or .dib images to be transferred from the Picture property of a form or picture box, and to be manipulated and transferred onto the Printer object.

Syntax: oPrinter.PaintPicture picture, x1, y1, width1, height1, x2, y2, width2, height2, opcode


PSet

Outputs a single pixel of a given color to the Printer object at a given point.

Syntax: oPrinter.PSet [Step] (x, y),[color]


Scale

Sets the coordinate system for the printer object. If no arguments are passed to the method, the scale mode is reset to twips.

Syntax: oPrinter.Scale [(x1, y1) - (x2, y2)]


ScaleX, ScaleY

Converts the width or height of the Printer object from one scale mode to another.

Syntax: oPrinter.ScaleX (width, fromscale, toscale)

Syntax: oPrinter.ScaleY (height, fromscale, toscale)


TextHeight

Returns the width in pixels that the given text will take up on the printer object using the current font settings.

Syntax: oPrinter.TextHeight (string)


TextWidth

Returns the width in pixels that the given text will take up on the printer object using the current font settings. Due to a bug in the initial release of VB5, the TextWidth property gave inconsistent results when run within the VB IDE. This problem was rectified with Service Pack 2.

Syntax: oPrinter.TextWidth (string)

Printers Collection

  • The Printers collection represents all printers installed on the current system. You can iterate through the Printers collection either by ordinal number or by using the For Each...Next statement. For example:

    For i = 0 To Printers.Count - 1
        Debug.Print Printers(i).DeviceName
    Next i
        
    For Each oPrinter In Printers
        Debug.Print oPrinter.DeviceName
    Next

  • You can use the Printers collection to find a specific printer that provides particular functionality, then assign that printer to the global Printer object using the Set statement. For example, you may need a printer that handles duplex printing:

    For Each oPrinter In Printers
        If oPrinter.Duplex Then
            Set Printer = oPrinter
    		 End If
    Next

    Or

    For i = 0 to Printers.Count -1
        If Printers(i).Duplex Then
            Set Printer = Printers(i)
        End If
    Next

  • A bug in VB5 prevented you from assigning a printer from the collection to a locally defined Printer object variable. This was corrected in VB5 Service Pack 1. For example:

    Dim oPrinter As Printer
    Set oPrinter = Printers(2)

  • If you reference a printer using the Collection object, the properties of that printer are read-only. To access printer properties on a read-write basis, you have to assign the reference to the Printer object, thereby making it the default printer.

  • The Printers collection has one property, Count, which returns the number of printers connected to the current machine. Remember, however, that the Printers collection is 0-based, unlike standard collection objects, which are 1-based. Therefore, you iterate through the Printers collection from to Count–1.

  • The Printers collection's Item method is its default method and therefore can be used implicitly as shown above.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.234.70