?
Solved

Remapping network printers

Posted on 2005-03-09
8
Medium Priority
?
526 Views
Last Modified: 2012-06-27
I have 2 printers in an office here that i need to re map.
One is being de commissioned and the other moving into production.
We have changed the name slightley and have made a script to remove the old printers and add the new one.
What i need to add is something that will check and see if the printer is mapped or not already, and if its not mapped to the users computer then to skip removing the printer and adding it.

Here is the script i have now :
' Removing SHARP Sugarplumb

Dim WshPrinter, strPrinterPath
strPrinterPath1 = "\\caedmfsrv1\Sugarplumb"
strPrinterPath2 = "\\caedmfsrv1\FieryXP12"
Set WshPrinter = WScript.CreateObject("WScript.Network")
WshPrinter.RemovePrinterConnection strPrinterPath1, true, true  
WScript.Echo "Removed Printer: " & strPrinterPath1
WshPrinter.RemovePrinterConnection strPrinterPath2, true, true  
WScript.Echo "Removed Printer: " & strPrinterPath2

' Adding XEROX Sugarplumb
Dim objNetwork, strPrintPath
strPrintPath = "\\caedmfsrv1\Sugarplum"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strPrintPath
WScript.Echo "Connected Printer: " & strPrintPath
Wscript.Quit
' End of Script

So essentially the script should check to see if \\caedmfsrv1\Sugarplumb exists and if it does remove it then check to see if \\caedmfsrv1\FieryXP12 is there and remove it. Then if it removed 1 of those 2 then map the new printer.
0
Comment
Question by:mkps
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 13501341
Option Explicit

'*******************************************************************
'   Name        :   modPrinters
'   Purpose     :   Provides functions for manipulating system
'                   printers
'*******************************************************************

'Declare API functions
Public Declare Function DeletePrinterConnection Lib "winspool.drv" Alias "DeletePrinterConnectionA" (ByVal pName As String) As Long

Public Function GetDefaultPrinter() As String

'*******************************************************************
'   Name        :   GetDefaultPrinter
'   Purpose     :   Retrieves the path of the current default printer
'                   on the local machine
'   Parameters  :   None
'   Returns     :   Default printer if successful, empty string if not
'*******************************************************************

'Declare local variables
Dim strReturn   As String
Dim prnDefault  As Printer

'Declare local constants
Const FUNCTION_NAME As String = "modPrinters.GetDefaultPrinter"
   
On Error GoTo ErrorHandler
           
    'Set default return value
    strReturn = vbNullString
           
    'Get the current default printer
    Set prnDefault = Printer
   
    'Get the name of the default printer
    strReturn = prnDefault.DeviceName
       
CleanExit:

    'Kill the printer object
    If Not prnDefault Is Nothing Then
        Set prnDefault = Nothing
    End If

    GetDefaultPrinter = strReturn

    Exit Function

ErrorHandler:

    'Display the error
    MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
           "Has occured in " & FUNCTION_NAME & vbCrLf & _
           "Please contact Technical Support", vbOKOnly + vbCritical, App.Title
   
    Resume CleanExit
   
End Function

Public Function SetDefaultPrinter(ByVal v_strPrinterPath As String) As Boolean

'*******************************************************************
'   Name        :   SetDefaultPrinter
'   Purpose     :   Sets the default printer on the local machine
'   Parameters  :   v_strPrinterPath  : Path of printer to set as default
'   Returns     :   TRUE if successful, FALSE if not
'*******************************************************************

'Declare local variables
Dim blnReturn   As Boolean
Dim objNetwork  As Object
   
'Declare local constants
Const FUNCTION_NAME As String = "modPrinters.SetDefaultPrinter"
   
On Error GoTo ErrorHandler

    'Set the default return value
    blnReturn = False
   
    'Create the Script Host object
    Set objNetwork = CreateObject("WScript.Network")
   
    'Set the printer to be the default
    objNetwork.SetDefaultPrinter v_strPrinterPath
   
    'Printer set
    blnReturn = True

CleanExit:

    'Kill the script host object
    If Not objNetwork Is Nothing Then
        Set objNetwork = Nothing
    End If

    SetDefaultPrinter = blnReturn

    Exit Function

ErrorHandler:

    'Display the error
    MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
           "Has occured in " & FUNCTION_NAME & vbCrLf & _
           "Please contact Technical Support", vbOKOnly + vbCritical, App.Title
   
    Resume CleanExit
   
End Function

Public Function AddPrinter(ByVal v_strPrinterPath As String, _
                           Optional ByVal v_strPrinterDriver = "") As Boolean

'*******************************************************************
'   Name        :   AddPrinter
'   Purpose     :   Adds the specified printer to the local machine
'   Parameters  :   v_strPrinterPath  : Path of printer to add
'                   v_strPrinterDriver: Driver of printer to add (Optional)
'   Returns     :   TRUE if printer added, FALSE if not
'*******************************************************************

'Declare local variables
Dim blnReturn   As Boolean
Dim objNetwork  As Object
   
'Declare local constants
Const FUNCTION_NAME As String = "modPrinters.AddPrinter"
   
On Error GoTo ErrorHandler

    'Set the default return value
    blnReturn = False
   
    'Create the Script Host object
    Set objNetwork = CreateObject("WScript.Network")
   
    'Add the printer
    objNetwork.AddWindowsPrinterConnection v_strPrinterPath, v_strPrinterDriver
       
    'Printer added and set
    blnReturn = True

CleanExit:

    'Kill the script host object
    If Not objNetwork Is Nothing Then
        Set objNetwork = Nothing
    End If

    AddPrinter = blnReturn

    Exit Function

ErrorHandler:

    'Display the error
    MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
           "Has occured in " & FUNCTION_NAME & vbCrLf & _
           "Please contact Technical Support", vbOKOnly + vbCritical, App.Title
   
    Resume CleanExit
   
End Function

Public Function RemovePrinter(ByVal v_strPrinterPath As String) As Boolean

'*******************************************************************
'   Name        :   RemovePrinter
'   Purpose     :   Removes the specified printer from the local
'                   machine
'                   NB: API function used as RemovePrinterConnection
'                   method of Windows Script Host did not work!
'   Parameters  :   v_strPrinterPath   : Printer to remove
'   Returns     :   TRUE if printer removed, FALSE if not
'*******************************************************************

'Declare local variables
Dim blnReturn   As Boolean
Dim lngReturn   As Long
   
'Declare local constants
Const FUNCTION_NAME As String = "modPrinters.RemovePrinter"
   
On Error GoTo ErrorHandler
       
    'Set the default return value
    blnReturn = False
       
    'Delete the printer
    lngReturn = DeletePrinterConnection(v_strPrinterPath)
   
    'Check the result
    If lngReturn <> 0 Then
   
        'Printer deleted
        blnReturn = True
       
    End If
   
CleanExit:

    RemovePrinter = blnReturn

    Exit Function

ErrorHandler:

    'Display the error
    MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
           "Has occured in " & FUNCTION_NAME & vbCrLf & _
           "Please contact Technical Support", vbOKOnly + vbCritical, App.Title
   
    Resume CleanExit
   
End Function

Public Function GetAllConnectedPrinters() As String()

'*******************************************************************
'   Name        :   GetAllConnectedPrinters
'   Purpose     :   Retrives all printers currently connected to
'                   the local machine
'   Parameters  :   None
'   Returns     :   String array containing printer names
'*******************************************************************

'Declare local variables
Dim bytIndex    As Byte
Dim objNetwork  As Object
Dim objPrinters As Object
Dim bytCounter  As Byte
Dim strReturn() As String
   
'Declare local constants
Const FUNCTION_NAME As String = "modPrinters.GetAllConnectedPrinters"
   
On Error GoTo ErrorHandler

    'Set the index counter
    bytIndex = 0
   
    'Create the Script Host object
    Set objNetwork = CreateObject("WScript.Network")
   
    'Retrieve the printer details
    Set objPrinters = objNetwork.EnumPrinterConnections
   
    For bytCounter = 0 To objPrinters.Count - 1 Step 2
       
        'Re dimesion the array
        ReDim Preserve strReturn(bytIndex)
           
        'Populate the array
        strReturn(bytIndex) = objPrinters.Item(bytCounter + 1)
       
        'Increment the index
        bytIndex = bytIndex + 1
   
    Next bytCounter
   
CleanExit:

    'Kill the script host object
    If Not objNetwork Is Nothing Then
        Set objNetwork = Nothing
    End If

    'Kill the printers object
    If Not objPrinters Is Nothing Then
        Set objPrinters = Nothing
    End If

    GetAllConnectedPrinters = strReturn()

    Exit Function

ErrorHandler:

   'Display the error
    MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
           "Has occured in " & FUNCTION_NAME & vbCrLf & _
           "Please contact Technical Support", vbOKOnly + vbCritical, App.Title
   
    Resume CleanExit
   
End Function

Public Function IsPrinterConnected(ByVal v_strPrinterPath As String) As Boolean

'*******************************************************************
'   Name        :   IsPrinterConnected
'   Purpose     :   Checks to see if the specified printer is
'                   connected to the local machine
'   Parameters  :   v_strPrinterPath   : Printer to find
'   Returns     :   TRUE if printer connected, FALSE if not
'
'*******************************************************************

'Declare local variables
Dim blnReturn   As Boolean
Dim objNetwork  As Object
Dim objPrinters As Object
Dim bytCounter  As Byte
   
'Declare local constants
Const FUNCTION_NAME As String = "modPrinters.IsPrinterConnected"
   
On Error GoTo ErrorHandler

    'Set the default return value
    blnReturn = False

    'Create the Script Host object
    Set objNetwork = CreateObject("WScript.Network")
   
    'Retrieve the printer details
    Set objPrinters = objNetwork.EnumPrinterConnections
   
    For bytCounter = 0 To objPrinters.Count - 1
       
        'Check for the specified printer
        If objPrinters.Item(bytCounter) = v_strPrinterPath Then
       
            'Printer Found
            blnReturn = True
            Exit For
           
        End If
       
    Next bytCounter
   
CleanExit:

    'Kill the script host object
    If Not objNetwork Is Nothing Then
        Set objNetwork = Nothing
    End If

    'Kill the printers object
    If Not objPrinters Is Nothing Then
        Set objPrinters = Nothing
    End If

    IsPrinterConnected = blnReturn

    Exit Function

ErrorHandler:

   'Display the error
    MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
           "Has occured in " & FUNCTION_NAME & vbCrLf & _
           "Please contact Technical Support", vbOKOnly + vbCritical, App.Title
   
    Resume CleanExit
   
End Function
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 13501374
'ADD THE ABOVE TO A .BAS MODULE
' And call each desired funtion
0
 
LVL 2

Author Comment

by:mkps
ID: 13501397
Sorry,
I am kinda new to this whole thing, essentially I need this to run in a login script, im not really sure at this point how to compile all of that great code into somthing that cause the desired result.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 13501400
okay...you mean a .VBS file?
0
 
LVL 2

Author Comment

by:mkps
ID: 13501406
yeah sorry
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 1500 total points
ID: 13505360
You need to use EnumPrinterConnections Method


http://www.winguides.com/scripting/reference.php?id=132
0
 
LVL 2

Author Comment

by:mkps
ID: 13510697
So that will tell me what printers are mapped.... How do i go about getting it to check if one is installed and then to remove it.
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 13534249
you need to create function and compare the printers.item(i+1)



0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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

800 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