Solved

VB code for adding a network printer

Posted on 2008-06-26
4
3,039 Views
Last Modified: 2008-06-26
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
Comment
Question by:knightrider2k2
  • 2
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
Frosty555 earned 500 total points
ID: 21877821
Try replacing

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

with

    Dim networkpath As String = """" & Server & " \ " & printername & """"
0
 
LVL 7

Author Comment

by:knightrider2k2
ID: 21878003
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
 
LVL 31

Expert Comment

by:Frosty555
ID: 21878236
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
 
LVL 7

Author Comment

by:knightrider2k2
ID: 21880083
It worked! (I had a space between " \ ")

Thanks,
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now