|
-
Jul 21st, 2007, 04:30 PM
#1
Printers Collection
Hello
I've been making a print module for a program I'm working on, and I figured it would be somewhat cleaner to avoid setting the built-in Printer object and just use my own. However, whenever I try to use any methods of my own Printer object, I get error 438: Object doesn't support this property or method.
Ex:
vb Code:
Dim MyPrinter As Printer
For Each MyPrinter In Printers
MsgBox MyPrinter.DeviceName 'this always works
MsgBox MyPrinter.ScaleX(15, vbTwips, vbInches) 'this errors
Next
However, the following works just fine
vb Code:
Dim MyPrinter As Printer
For Each MyPrinter In Printers
Set Printer = MyPrinter
MsgBox Printer.DeviceName 'this always works
MsgBox Printer.ScaleX(15, vbTwips, vbInches) 'this no longer errors
Next
I would think that any Printer object should support the same set of methods... so this is really confusing me. I know I could work around it by just using the Printer object instead, but in any case I'd like to know what's going on.
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Jul 21st, 2007, 05:47 PM
#2
Re: Printers Collection
you would need to set myprinter to be the current printer to be able to set it
anything you do with the printer object is only local to your program, and does not change any default settings for other programs
vb Code:
1.
Dim MyPrinter As Printer
For Each MyPrinter In Printers
If MyPrinter.DeviceName "Canon" Then ' or whatever other printer you want to use
Set Printer = MyPrinter
Exit For
End If
Next
this allows you to set which printer you want to use for your application then it is the Printer Object for your program
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
-
Jul 21st, 2007, 05:52 PM
#3
Re: Printers Collection
That's because you cannot set values to any property within collection - as you found out for yourself it could only be done outside collection.
-
Jul 24th, 2007, 12:46 AM
#4
Re: Printers Collection
Sorry, I'm still not getting why this doesn't work
I know I COULD use the built in Printer variable/object, but I don't want to if it can be avoided (mainly because most of the code is already written and it would take a slight revision to change it--though, it wouldn't take very long). The thing I don't get is how one instance of a Printer object (the Printer variable) supports certain methods like ScaleX, while another instance of a Printer object (MyPrinter in the loop I posted before) doesn't.
That's because you cannot set values to any property within collection - as you found out for yourself it could only be done outside collection.
I can't see that I'm setting any properties in that code?? I understand that the control (loop) variable is just a reference to the object that the collection refers to, so Setting that control variable wouldn't do anything to the collection.
Thanks for your replies anyway!
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Jul 24th, 2007, 01:57 AM
#5
Re: Printers Collection
only the current printer is the printer object, you can set an object to represent the printer object, but members of the collection are not the printer object and do not have the methods or properties of the printer object
scalex is obviously a property of the printer object, while all members of the printer collection have a name property
if you
set myprinter = printer
then your code should work
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
-
Jul 24th, 2007, 04:38 AM
#6
Re: Printers Collection
Thank you for your comments westconn1
you can set an object to represent the printer object, but members of the collection are not the printer object and do not have the methods or properties of the printer object
That statement gets at the heart of my question, but I'm confused by it. From using the TypeName function on members of the Printers collection, every member of the Printers collection is of type Printer, which is exactly the same type as the Printer global variable.
So, I guess my problem boils down to this: how can one instance of a particular object support a certain method, while another instance of the same object doesn't?
And also, whenever I Set the Printer global variable to refer to one of the members of the Printers collection, am I not simply saying that when I use the variable "Printer", what I'm referring to is actually the member of the Printers collection from before? And in that case, shouldn't each member of the Printers collection have every method that is normally used by the Printer global variable?
In general I would think that the following should always produce two identical message boxes {so long as the method used returns the same output each time it is called}:
vb Code:
Dim MyReference As MyClassType
Dim MyObject As New MyClassType
SetProperties MyObject 'set whatever properties are required
Set MyReference = MyObject
MsgBox MyObject.AddTwoPlusTwo 'AddTwoPlusTwo can be replaced by any method
MsgBox MyReference.AddTwoPlusTwo
In this case, MyReference is the Printer global variable, MyClassType is Printer, MyObject is a member of the Printers collection, MyObject is declared and SetProperties is called by Visual Basic in populating the Printers collection, presumably on program startup, and AddTwoPlusTwo can be replaced by ScaleX:
vb Code:
'Dim MyReference As MyClassType ... becomes ...
'<behind-the-scenes> Global Printer as Printer
'Dim MyObject As New MyClassType ... becomes ...
'<behind-the-scenes> Global MyPrinter As New Printer
'<behind-the-scenes> Printers.Add MyPrinter
'SetProperties MyObject 'set whatever properties are required ... becomes ...
SetProperties MyPrinter
'Set MyReference = MyObject ... becomes ...
Set Printer = MyPrinter
'MsgBox MyObject.AddTwoPlusTwo 'AddTwoPlusTwo can be replaced by any method ... becomes ...
MsgBox MyPrinter.ScaleX(15, vbInches, vbInches)
'MsgBox MyReference.AddTwoPlusTwo ... becomes ...
MsgBox Printer.ScaleX(15, vbInches, vbInches)
However, the first message box fails with error 438 if this code is run. Whhhyyyyyy??????
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Jul 24th, 2007, 08:56 PM
#7
Re: Printers Collection
try it round the other way
Code:
#
Set MyPrinter = Printer
MsgBox MyPrinter.ScaleX(15, vbInches, vbInches)
MsgBox Printer.ScaleX(15, vbInches, vbInches)
you have to set myprinter to be the printer object first, myprinter may then need to be declared as an object rather than a printer
i use code that will either print to the printer or a form using the same code, by setting an object to be either the form or the printer, then using the properties and methods of the object, so printerobject.show will work if the object has been set as a form, but not if it set as a printer, for which i would use objectprinter.enddoc, all the rest of th code is the same
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
-
Jul 31st, 2007, 04:32 PM
#8
Re: Printers Collection
That's actually what I ended up doing (since I needed to be able to print to a picturebox as well as the printer), however, if you have a printer selection dialog, somewhere before the you Set MyPrinter = Printer, you'll have to Set Printer = SelectedPrinter. But the weird thing is, "SelectedPrinter" is an object of type "Printer" (exactly like the "Printer" variable), but it doesn't support certain methods (ex. ScaleX).
vb Code:
'ShowPrinterDialog() is a function [that I've modified from the API Guide] of type Object.
'After the user makes a selection, ShowPrinterDialog() is set to the appropriate object in the Printers collection
MsgBox TypeName(ShowPrinterDialog()) 'says "Printer"
MsgBox ShowPrinterDialog().ScaleX(15, vbPixels, vbPixels) 'errors!!!
MsgBox TypeName(Printer) 'says "Printer"
MsgBox Printer.ScaleX(15, vbPixels, vbPixels) 'succeeds just fine
I really understand how to work around this, that isn't the problem. I'm just confused on how two different instances of the same type of object can have different methods--is this some weird thing about VB6 programming?
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
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
|