Solved

VB code for adding a network printer

Posted on 2008-06-26
4
2,972 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
Comment Utility
Try replacing

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

with

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

Author Comment

by:knightrider2k2
Comment Utility
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
Comment Utility
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
Comment Utility
It worked! (I had a space between " \ ")

Thanks,
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now