• Printer Object

    Author
    Topic
    #356306

    This was a simple request…..
    I have been asked to ensure that a printer (by default) only prints one copy of any document.
    My code:
    Dim objDefPrntr As Printer

    Set objDefPrntr = Printers(0)

    objDefPrntr.Copies = 1
    returns an error that ‘Copies’ is read only. error 383

    can anyone explain to me why this is read only at runtime please and also give me a clue as to how I can enforce the requirement?

    The need stems from a rogue application in use that leaves the Copies default to whatever the last setting was and so causes a lot of paper waste as people forget to reset the value before printing form other apps.

    Viewing 0 reply threads
    Author
    Replies
    • #527082

      If you use the PrintOut method, you can set the number of copies:

      Application.PrintOut FileName:=””, range:=wdPrintAllDocument, _
      Item:= wdPrintDocumentContent, Copies:=3, Pages:=””

      I take it that the property of the object is read/only until you actually use the method on the object to print something.

      • #527119

        I think this may be Word ?
        how does this translate into VB6 ?

        • #527126

          According to the VB6 help the printer.copies property is read/write. I’ve tested it in IDE and run the code and it appear to work. I haven’t tried printing, but it does not give me a read only message. Maybe runtime is different.

          • #527149

            I tried this in VB6 and it worked just fine, no matter what number I set for the copies.

            Private Sub mnuPrint_Click()
              Dim strMsg As String
              Dim objPrn As Object
              
              Set objPrn = Printer
              With objPrn
                .Copies = 1
              End With
              
              strMsg = "This tests the copies property of Printer." & vbCrLf _
                      & "I am printing " & objPrn.Copies & " of this message."
                      
              Printer.Print strMsg
              Printer.EndDoc
              Set objPrn = Nothing
            End Sub 'mnuPrint_Click()
            • #527168

              Charlotte,
              This is running without the error – many thanks.
              What was wrong with my code ? Why does
              Dim objPrn as object
              set objPrn = printer
              work better than
              dim objPrn as printer
              set objprn = printer(0)?
              is my version incorrect (other than it doesnt work) in its structure ?

            • #527191

              The difference is that in the late binding I used, Printer is the current printer, while you’re trying to specify a printer. On-line help says this:[indent]


              If you use the Printers collection to specify a particular printer, as in Printers(3), you can only access properties on a read-only basis. To both read and write the properties of an individual printer, you must first make that printer the default printer for the application.


              [/indent]

            • #527193

              Thanks, a demonstration of late versus early binding that makes sense to me

            • #527285

              The only reason I used late binding was because I was setting the object variable to the default printer, so I didn’t really need to dim the object as a printer and then specify which printer. If I were, I’d probably use early binding so intellisense would kick in.

    Viewing 0 reply threads
    Reply To: Printer Object

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: