ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA API Default printer (https://www.excelbanter.com/excel-programming/399466-vba-api-default-printer.html)

ArmsteR

VBA API Default printer
 
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...b;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


urkec

VBA API Default printer
 
"ArmsteR" wrote:

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...b;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.

--
urkec


All times are GMT +1. The time now is 05:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com