Results 1 to 12 of 12

Thread: Printer problems

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Printer problems

    I'm stumped by the error I'm getting here - this code finds the DYMO label writer installed on a machine and successfully prints a label...

    Code:
    Dim prtPrinter As Printer, prtPriorPrinter As Printer
    
    For Each prtPrinter In Printers
        If Left(prtPrinter.DeviceName, 4) = "DYMO" Then
            Set prtPriorPrinter = Printer
            Set Printer = prtPrinter
            Printer.Orientation = vbPRORLandscape
            Printer.FontSize = 10
            Printer.Print "Line 1"
            Printer.Print "Line 2"
            Printer.EndDoc
            Set Printer = prtPriorPrinter <- The error occurs at this line ***
            Exit For
        End If
    Next
    But I get the error Type Mismatch at the line of code noted.

    What am I doing wrong?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Printer problems

    Is Printer set to value (ie not nothing?)

  3. #3

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Printer problems

    Yes - in the watches window I can see prtPriorPrinter be NOTHING until the loop hits the dymo

    I can open the PRINTER object in the watch window - type of Object/Printer

    The prtPriorPrinter object I can also open - type of Printer/Printer

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: Printer problems

    You
    Set prtPriorPrinter = Printer
    which makes any changes in Printer reflect into prtPriorPrinter. It is not a separate Printer variable.
    You then
    Set Printer = prtPrinter
    which makes prtPriorPrinter reflect prtPrinter

    so even if Set Printer = prtPriorPrinter executed without an error it would not do what you want to do.

    Try;
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    
        Dim prtPrinter As Printer, prtPriorPrinter As String
        
        For Each prtPrinter In Printers
            If prtPrinter.DeviceName = "Microsoft Office Document Image Writer" Then
                prtPriorPrinter = Printer.DeviceName
                Set Printer = prtPrinter
                'Printer.Orientation = vbPRORLandscape
                Printer.FontSize = 10
                Printer.Print "Line 1"
                Printer.Print "Line 2"
                Printer.EndDoc
                Exit For
            End If
        Next
        
        For Each prtPrinter In Printers
            If prtPrinter.DeviceName = prtPriorPrinter Then
                Set Printer = prtPrinter
                Exit For
            End If
        Next
            
    End Sub

  5. #5
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Printer problems

    Quote Originally Posted by Magic Ink View Post
    You
    Set prtPriorPrinter = Printer
    which makes any changes in Printer reflect into prtPriorPrinter. It is not a separate Printer variable.
    You then
    Set Printer = prtPrinter
    which makes prtPriorPrinter reflect prtPrinter
    Does it? I'm not calling your bluff on this but surely it does not change a reference to another reference (like a pointer to a pointer). This would have broke a huge number of my apps if it was true.

  6. #6

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Printer problems

    I kind of thought something like that was going on - I fixed it like this

    Code:
    For Each prtPriorPrinter In Printers
        If prtPriorPrinter.DeviceName = Printer.DeviceName Then
            For Each prtPrinter In Printers
                If Left(prtPrinter.DeviceName, 4) = "DYMO" Then
                    Set Printer = prtPrinter
                    Printer.Orientation = vbPRORLandscape
                    Printer.FontSize = 10
                    Printer.FontBold = True
                    Printer.Print strName
                    Printer.Print strAddr1
                    Printer.Print strAddr2
                    Printer.Print strAddr3
                    Printer.Print strAddr4
                    Printer.EndDoc
                    Exit For
                End If
            Next
            Set Printer = prtPriorPrinter
            Exit For
        End If
    Next

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Printer problems

    Does it? I'm not calling your bluff on this but surely it does not change a reference to another reference (like a pointer to a pointer). This would have broke a huge number of my apps if it was true.
    yes it will change with changes to the printer object, simple to test

    vb Code:
    1. set myprinter = printer
    2. msgbox myprinter.devicename
    3. set printer = printers(0)
    4. msgbox myprinter.devicename
    5. set printer = printers(1)
    6. msgbox myprinter.devicename
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: Printer problems

    Grimfort,
    Step through the original code and watch prtPriorPrinter.DeviceName

  9. #9
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Printer problems

    How bizzare. It does not work like this in .Net, maybe I have been away from VB6 too long . How do you dereference an object without setting that to nothing.

    Why does this not follow the same rules
    Code:
    Dim myPrinter As Printer
    Set myPrinter = Printer
    MsgBox myPrinter.DeviceName
    Set Printer = Printers(1)
    MsgBox myPrinter.DeviceName
    Set myPrinter = Printers(0)
    MsgBox Printer.DeviceName
    Instead of setting the Printer, I changed the myPrinter which was set the same way as before, except this time Printer did not change. Difference between ByRef/ByVal ? Confused .

  10. #10

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Printer problems

    Quote Originally Posted by Grimfort View Post
    How bizzare. It does not work like this in .Net, maybe I have been away from VB6 too long...
    Exactly - this is the only VB6 app I still support - I wrote it in 2001 - too much .Net since then for me as well...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  11. #11
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Printer problems

    Hmm, well, I retested using a locally created object instead of the Printer object and it works as it does in .Net. When you set a variable to the Printer property it must be to the property itself and not the object that the property points to. I guess I have not come across this.

  12. #12
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: Printer problems

    The Printer Object is a little unusual in that there is only one of them, you cannot use New or CreateObject to make new active instances of it. Any variables you set to it reflect the single instance and Properties of those variables are read only, the Methods are unusable.
    So the Printer is hardly an object at all, it is just made to behave a bit like one in vb6.

    >using a locally created object
    They do not behave differently except that their Properties are read/ write and their Methods work.

    Code:
    Private Sub Command4_Click()
    
        Dim Obj1 As Object
        Dim Obj2 As Object
        Dim Obj3 As Object
    
        MsgBox (Command4.Caption)
        Set Obj1 = Command4
        Set Obj2 = Obj1
        Set Obj3 = Obj2
        
        Obj3.Caption = "Something else"
        
        MsgBox (Command4.Caption)
    
    End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width