?
Solved

VB code for adding a network printer

Posted on 2008-06-26
4
Medium Priority
?
3,431 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
Frosty555 earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

718 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