cancel
Showing results for 
Search instead for 
Did you mean: 

Set Default printer - Blueprism

HemalathaPolasa
Level 2
Hi,

Do anyone have the code stage that have been already developed for choosing the default printer or else can you suggest me what would be the best way to do?

Thanks!
1 BEST ANSWER

Helpful Answers

KotaSuto1
Level 5
Hi,

I've never deployed to Production, but this code will work:
(Beforehand add "System.Management.dll" to External References and "System.Management" to Name Space Imports.)
' pn is an Input Parameter, which is the printer name to set as default.

If pn = "" Then
	return
End If

Dim mos As New ManagementObjectSearcher( _
	"SELECT * FROM Win32_Printer")
Dim moc As ManagementObjectCollection = mos.Get()
Dim mo As ManagementObject
For Each mo In moc
	If CStr(mo("Name")) = pn Then
		Dim mbo As ManagementBaseObject = _
			mo.InvokeMethod("SetDefaultPrinter", Nothing, Nothing)
		Dim ret As Long = Convert.ToInt64(mbo("returnValue"))
		If ret <> 0 Then
			Throw New Exception("Failed to set default printer.")
		End If
		return
	End If
Next mo

Throw New Exception("Printer not found.")​


I hope this will help 🙂



------------------------------
Kota Suto
Japan
------------------------------

View answer in original post

3 REPLIES 3

KotaSuto1
Level 5
Hi,

I've never deployed to Production, but this code will work:
(Beforehand add "System.Management.dll" to External References and "System.Management" to Name Space Imports.)
' pn is an Input Parameter, which is the printer name to set as default.

If pn = "" Then
	return
End If

Dim mos As New ManagementObjectSearcher( _
	"SELECT * FROM Win32_Printer")
Dim moc As ManagementObjectCollection = mos.Get()
Dim mo As ManagementObject
For Each mo In moc
	If CStr(mo("Name")) = pn Then
		Dim mbo As ManagementBaseObject = _
			mo.InvokeMethod("SetDefaultPrinter", Nothing, Nothing)
		Dim ret As Long = Convert.ToInt64(mbo("returnValue"))
		If ret <> 0 Then
			Throw New Exception("Failed to set default printer.")
		End If
		return
	End If
Next mo

Throw New Exception("Printer not found.")​


I hope this will help 🙂



------------------------------
Kota Suto
Japan
------------------------------

Thanks a lot, It worked 🙂


Hi Kota ,

Hope you are doing good, Do you know how to set network printer as default?

The Code is able to set default only which are available in windows  



------------------------------
Raja Sekhar Reddy Palam
Automation Analyst
Gateshead Health NHS Foundation Trust
Gateshead
07852757441
------------------------------