Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3552
  • Last Modified:

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,
0
knightrider2k2
Asked:
knightrider2k2
  • 2
  • 2
1 Solution
 
Frosty555Commented:
Try replacing

    Dim networkpath As String = Server & " \ " & printername

with

    Dim networkpath As String = """" & Server & " \ " & printername & """"
0
 
knightrider2k2Author Commented:
No, This did not help. Gives the same error "printer name was typed incorrectly blah blah blah"
This is even giving error on printers without any spaces in the name.
0
 
Frosty555Commented:
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.
0
 
knightrider2k2Author Commented:
It worked! (I had a space between " \ ")

Thanks,
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now