|
-
Aug 12th, 2010, 05:45 AM
#1
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?
-
Aug 12th, 2010, 05:49 AM
#2
Re: Printer problems
Is Printer set to value (ie not nothing?)
-
Aug 12th, 2010, 05:53 AM
#3
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
-
Aug 12th, 2010, 06:44 AM
#4
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
-
Aug 12th, 2010, 06:57 AM
#5
Re: Printer problems
 Originally Posted by Magic Ink
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.
-
Aug 12th, 2010, 06:58 AM
#6
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
-
Aug 12th, 2010, 07:12 AM
#7
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:
set myprinter = printer msgbox myprinter.devicename set printer = printers(0) msgbox myprinter.devicename set printer = printers(1) 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
-
Aug 12th, 2010, 07:13 AM
#8
Re: Printer problems
Grimfort,
Step through the original code and watch prtPriorPrinter.DeviceName
-
Aug 12th, 2010, 08:08 AM
#9
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 .
-
Aug 12th, 2010, 08:10 AM
#10
Re: Printer problems
 Originally Posted by Grimfort
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...
-
Aug 12th, 2010, 08:22 AM
#11
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.
-
Aug 12th, 2010, 08:55 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|