Solved

VB code for adding a network printer

Posted on 2008-06-26
4
3,102 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

803 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