knightrider2k2
asked on
VB code for adding a network printer
Hi
I am trying to write a small application in VB (just started learning VB today) to add network printers. I found some code on the internet
Dim printername As String = CStr(ListBox1.SelectedItem )
Dim Server As String = "\\server"
Dim networkpath As String = Server & " \ " & printername
Dim useExistingDriver As Boolean = 0
Dim setdefaultprinter As Boolean = 0
Dim cmdToSend As String = "rundll32 printui.dll,PrintUIEntry /in /q /n " & networkpath
If useExistingDriver Then cmdToSend += " /u"
If setDefaultPrinter Then cmdToSend += " /y" '
Shell(cmdToSend, AppWinStyle.Hide) ' execute the command
This is working, but if I have "Spaces" in the printer name, it gives error. Please help.
FYI, I am not using VBscript because I need GUI(comboboxes etc)
Thanks,
I am trying to write a small application in VB (just started learning VB today) to add network printers. I found some code on the internet
Dim printername As String = CStr(ListBox1.SelectedItem
Dim Server As String = "\\server"
Dim networkpath As String = Server & " \ " & printername
Dim useExistingDriver As Boolean = 0
Dim setdefaultprinter As Boolean = 0
Dim cmdToSend As String = "rundll32 printui.dll,PrintUIEntry /in /q /n " & networkpath
If useExistingDriver Then cmdToSend += " /u"
If setDefaultPrinter Then cmdToSend += " /y" '
Shell(cmdToSend, AppWinStyle.Hide) ' execute the command
This is working, but if I have "Spaces" in the printer name, it gives error. Please help.
FYI, I am not using VBscript because I need GUI(comboboxes etc)
Thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I tried typing the rundll command directly into a command window and it seems to work okay.
Use msgbox() to output the value of the cmdToSend variable just before you execute it and put what it was here.
Use msgbox() to output the value of the cmdToSend variable just before you execute it and put what it was here.
ASKER
It worked! (I had a space between " \ ")
Thanks,
Thanks,
ASKER
This is even giving error on printers without any spaces in the name.