Discussion:
How to set Paper size in vbs
(too old to reply)
Subbu
2009-08-12 07:03:01 UTC
Permalink
Hi all,
could anyone suggest a way to set the paper size in default printer in vbs.
This is not for MSoffice printing, but a 3rd party ECAD software, which
supports vbs.


I have used WMInterface to do this, but it is not setting the paper
size.(below is the code)

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from
Win32_PrinterConfiguration",,48)
For Each objItem in colItems
Wscript.Echo "PaperSize: " & objItem.PaperSize
objItem.PaperSize = 8
Wscript.Echo "PaperSize: " & objItem.PaperSize
Next

It is outputting paper size as 8(A3) but, it does not printout in A3.

Can anyone suggest the correct way of setting the papersize or printer
properties?

thanks
Alex K. Angelopoulos
2009-08-12 07:55:53 UTC
Permalink
Unfortunately, that technique won't work. The Win32_PrinterConfiguration
properties are read-only. You can set the PaperSize value because you're not
dealing with the PrinterConfiguration itself, but with a cached value
collection.

For WMI classes which _do_ have writable properties, you can use WMI's
Put_() method to write a modified instance back to the repository. Of
course, in this example, it will fail. If you do this:

For Each objItem in colItems
Wscript.Echo "PaperSize: " & objItem.PaperSize
objItem.PaperSize = 8
objItem.Put_()
Wscript.Echo "PaperSize: " & objItem.PaperSize
Next

The Put_ call will throw the error "SWbemObjectEx: Provider is not capable
of the attempted operation".

================
Approaches to setting the paper size

This is a problem that people have asked about repeatedly over the years,
and there's never been a simple out-of-box solution. I'm not totally aware
of the reasons, but I suspect its due to the fact that this is theoretically
a WMI-ish problem, but the core WMI infrastructure has never supported
general control over printer configuration details; that in turn is likely
due to the limited and inconsistent exposure of properties by printer OEMs
as a whole.

You have three options I can think of to get the results you need. Follow up
on what you can and we'll see if we can help.

(1) Use your CAD pacakge's capabilities
You didn't mention the name of the software package you use, but since it
supports VBScript it's apparently designed to be automated, and may expose
details for available printers internally. Could you tell us what it is, and
possibly post a link to the manufacturer's website?
This is a good option as a solution, since CAD is one problem domain where
printer sheet size control is important.

(2) Direct registry edit from script
A second option is finding out where the printer configuration data is
stored in the registry and in what format, and then modifying it directly.
There are general printer configuration details stored under , but its also
common for graphics applications such as art and CAD packages to set
specific configurations in their own software keys in the registry (possibly
under HKCU\Software\<appname> somewhere).
If your application does _not_ do this, things get complicated - too
complicated to be a good idea in my opinion; I suggest skipping to (3).

(3) Custom component
There are components that can manipulate printer settings directly for you.
Lee Peedin (a REXX guru who occasionally hangs out with us here) has one
such object that may be useful; it's PrinterObject2, which can be found on
his page here:
http://pragmaticlee.safedataisp.net/
Post by Subbu
Hi all,
could anyone suggest a way to set the paper size in default printer in vbs.
This is not for MSoffice printing, but a 3rd party ECAD software, which
supports vbs.
I have used WMInterface to do this, but it is not setting the paper
size.(below is the code)
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from
Win32_PrinterConfiguration",,48)
For Each objItem in colItems
Wscript.Echo "PaperSize: " & objItem.PaperSize
objItem.PaperSize = 8
Wscript.Echo "PaperSize: " & objItem.PaperSize
Next
It is outputting paper size as 8(A3) but, it does not printout in A3.
Can anyone suggest the correct way of setting the papersize or printer
properties?
thanks
Subbu
2009-08-12 14:01:02 UTC
Permalink
Thanks Alex for the detailed reply.
The ECAD software is E3.Schematic from zuken. www.zuken.com. I did realise
the read only properties of wmi.

1. as mentioned the Ecad scripting interface is yet to support paper size.
2. It uses the printer's configuration/properties for paper size etc. i.e
we get printer dialog box for these. If I can access the printer's setting
in the registry then it may be possible.
3. I did download the Activex Printer object 2. I ran the registry update.
However I am not sure how to use this object in the script. it is throwing
errors. any sample code

Thanks for the info so far
regards
subbu
Post by Alex K. Angelopoulos
Unfortunately, that technique won't work. The Win32_PrinterConfiguration
properties are read-only. You can set the PaperSize value because you're not
dealing with the PrinterConfiguration itself, but with a cached value
collection.
For WMI classes which _do_ have writable properties, you can use WMI's
Put_() method to write a modified instance back to the repository. Of
For Each objItem in colItems
Wscript.Echo "PaperSize: " & objItem.PaperSize
objItem.PaperSize = 8
objItem.Put_()
Wscript.Echo "PaperSize: " & objItem.PaperSize
Next
The Put_ call will throw the error "SWbemObjectEx: Provider is not capable
of the attempted operation".
================
Approaches to setting the paper size
This is a problem that people have asked about repeatedly over the years,
and there's never been a simple out-of-box solution. I'm not totally aware
of the reasons, but I suspect its due to the fact that this is theoretically
a WMI-ish problem, but the core WMI infrastructure has never supported
general control over printer configuration details; that in turn is likely
due to the limited and inconsistent exposure of properties by printer OEMs
as a whole.
You have three options I can think of to get the results you need. Follow up
on what you can and we'll see if we can help.
(1) Use your CAD pacakge's capabilities
You didn't mention the name of the software package you use, but since it
supports VBScript it's apparently designed to be automated, and may expose
details for available printers internally. Could you tell us what it is, and
possibly post a link to the manufacturer's website?
This is a good option as a solution, since CAD is one problem domain where
printer sheet size control is important.
(2) Direct registry edit from script
A second option is finding out where the printer configuration data is
stored in the registry and in what format, and then modifying it directly.
There are general printer configuration details stored under , but its also
common for graphics applications such as art and CAD packages to set
specific configurations in their own software keys in the registry (possibly
under HKCU\Software\<appname> somewhere).
If your application does _not_ do this, things get complicated - too
complicated to be a good idea in my opinion; I suggest skipping to (3).
(3) Custom component
There are components that can manipulate printer settings directly for you.
Lee Peedin (a REXX guru who occasionally hangs out with us here) has one
such object that may be useful; it's PrinterObject2, which can be found on
http://pragmaticlee.safedataisp.net/
Post by Subbu
Hi all,
could anyone suggest a way to set the paper size in default printer in vbs.
This is not for MSoffice printing, but a 3rd party ECAD software, which
supports vbs.
I have used WMInterface to do this, but it is not setting the paper
size.(below is the code)
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from
Win32_PrinterConfiguration",,48)
For Each objItem in colItems
Wscript.Echo "PaperSize: " & objItem.PaperSize
objItem.PaperSize = 8
Wscript.Echo "PaperSize: " & objItem.PaperSize
Next
It is outputting paper size as 8(A3) but, it does not printout in A3.
Can anyone suggest the correct way of setting the papersize or printer
properties?
thanks
mr_unreliable
2009-08-12 18:10:34 UTC
Permalink
Post by Alex K. Angelopoulos
(3) Custom component
There are components that can manipulate printer settings directly for
you. Lee Peedin (a REXX guru who occasionally hangs out with us here)
has one such object that may be useful; it's PrinterObject2, which can
http://pragmaticlee.safedataisp.net/
As you probably know, Lee Peedin's printer object is nothing more
than a wrapper for the vb printer object. As you may also know,
the vb printer object is the most DIS-respected part of (classic)
vb, according many comments which appear (or did appear way back
when classic vb was commonly used).

In my earlier naive days, I also wrote a wrapper for the printer
object, usable from script. After a while using the printer object
I found the same frustrations and errors (gasp!) that others had
reported on. And so I followed the conventional advice, and now
do all my printing by using the system api -- which works fine.
I know what you're thinking -- It wasn't the printer object, it
was my wrapper that caused the problems. O.K., fair enough. The
only way to settle that is for you to try and use the printer
object for yourself.

That being said, for something simple like changing the paper
size, Lee Peedin's oontrol will probably work well enough.

cheers, jw
Eric
2009-08-18 19:42:22 UTC
Permalink
Post by Alex K. Angelopoulos
Unfortunately, that technique won't work. The Win32_PrinterConfiguration
properties are read-only. You can set the PaperSize value because you're
not dealing with the PrinterConfiguration itself, but with a cached value
collection.
You can do something like that in VB
(http://forums.devx.com/archive/index.php/t-65710.html).
I remember setting the printer orientation in my own VB program and finding
something of an object with read only properties I had to work around. Does
VBScript have something similar?
Subbu
2009-08-31 02:05:01 UTC
Permalink
Guys,
Thanks for the replies so far. I found out the way to accomplish this in
VBS.

1. you need to download Windows resource kit tools. This is freely
downloadable.

http://www.microsoft.com/Downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en

Have a look at the help file also, in Allprograms-->windows resource kit
tools->Windows resource kit tools help.html

2. One of the tools in this took kit is, Setprinter.exe, spooler
configuration tool

3. Then I used the following code in VBS to achieve this.
Dim Wshshell, setpnra3, myprinter

Set Wshshell= CreateObject("WScript.Shell")
myprinter = "printer name"
setpnra3 = "setprinter " &_
"""" & myprinter & """" & " 2 " & """" & "pdevmode=dmpapersize=8" & """"
' size = 8 for A3, size 9 for A4 etc.

intRC = WshShell.Run(setpnra3, 0, TRUE)

if intRC <> 0 then
WScript.Echo "Error returned from Setting the Printer " & intRC
WScript.Quit
End If

4. Setprinter.exe can set printer settings to different levels. I have
used level "2" which has most of the printer settings, in pDevmode.
Remember, this sets "myprinter" to this new settings.

5. For the remote printers, if the access is denied, then you can not set
using this command. Work around to this is, Install the Remote pritner
locally, to a new local port( there is a procedure in Microsoft web site) and
then change the settings to this local printer. I have done this way, it
works fine.

6. Using this setprinter, you can set most of the printer settings(this is
not application based, will change the printer settings globally).
The following can be set:- in pdevmode
dmorientation = 1 (P) or 2 (L)
dmpapersize=9(a4), 8(a3)etc
dmcopies=1
dmdefaultsource=7
dmprintquality=600
dmcolor=1monochrome 2 color
dmduplex=1no.2 yes
dmyresolution=600
dmTToption=2 (True type fonts)
dmcollate=1 on 2 off
dmbitsperpel =4
dmICMmethod =1
dmICMintent=3
dmMediatype=1 plain

Hope this helps. If you have any issues, I will be glad to help, If I know.

regards
Post by Eric
Post by Alex K. Angelopoulos
Unfortunately, that technique won't work. The Win32_PrinterConfiguration
properties are read-only. You can set the PaperSize value because you're
not dealing with the PrinterConfiguration itself, but with a cached value
collection.
You can do something like that in VB
(http://forums.devx.com/archive/index.php/t-65710.html).
I remember setting the printer orientation in my own VB program and finding
something of an object with read only properties I had to work around. Does
VBScript have something similar?
Loading...