VBA API Default printer

A

ArmsteR

Hi,

I have an issue with some code I'm trying to produce. Though at the
moment I feel like i wish i hadn't started! :p


I am producing a batch printing script for some drawing register
spreadsheets I have. Basically I need to be able to set the default
printer in WINDOWS from within VBA to different printers (printers
setup with the default print settings to different page sizes)
depending upon wether a certain cell contains the text A4-A3-A2-A1 or
A0.


These will be controling External documents from PDF's to AutoCAD
drawings and therefore this needs to be using the windows default
printer to print via the ShellExecute() and not the activeprinter
internal command.


I've looked around and found some VB script that look like it works
(but would need tweaking, but I just can't make any headway with
getting it to work in VB.


http://support.microsoft.com/default.aspx?scid=kb;en-us;266767


Can anyone help me? This is the final stage of this project thats
taken me a while! :) I'd appreciate any pointers at all :)


Dave
PS this has been posted elsewhere i'm just getting desperate :p lol
 
G

Guest

ArmsteR said:
Hi,

I have an issue with some code I'm trying to produce. Though at the
moment I feel like i wish i hadn't started! :p


I am producing a batch printing script for some drawing register
spreadsheets I have. Basically I need to be able to set the default
printer in WINDOWS from within VBA to different printers (printers
setup with the default print settings to different page sizes)
depending upon wether a certain cell contains the text A4-A3-A2-A1 or
A0.


These will be controling External documents from PDF's to AutoCAD
drawings and therefore this needs to be using the windows default
printer to print via the ShellExecute() and not the activeprinter
internal command.


I've looked around and found some VB script that look like it works
(but would need tweaking, but I just can't make any headway with
getting it to work in VB.


http://support.microsoft.com/default.aspx?scid=kb;en-us;266767


Can anyone help me? This is the final stage of this project thats
taken me a while! :) I'd appreciate any pointers at all :)


You can try this code, it uses WMI Win32_Printer class:



Sub SetDefaultPrinter()

'get printer name

strPrinterName = Cells(1, 1)

'connect to WMI locally

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!" _
& "root\cimv2")

'get WMI printer object

Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer Where Name = '" _
& strPrinterName & "'")

'try setting the default printer and report the result

If colInstalledPrinters.Count = 1 Then

For Each objPrinter In colInstalledPrinters
retVal = objPrinter.SetDefaultPrinter()
Next

If retVal = 0 Then
MsgBox "default printer is " & strPrinterName
Else
MsgBox "error setting the default printer"
End If

Else

MsgBox strPrinterName & " : printer not available"

End If

End Sub


Hope this helps and good luck with your project.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top