locked
Creating Print Queues Error - VBS RRS feed

  • Question

  • Hello,

    I have created a script for creating print queues on remote computer. The script goes fine but as soon as it executes    objPrinter.Put_, it gives "Runtime error '-2147217407 (80041001)'. When I try error handler, it gives error 13 - type mismatch. I have tried the following:

    1) Tried to create print queue on remote machine - same error

    2) Checked Print Drivers already exist  & tried with different drivers - same error

    3) Some forums mentioned to enter a comment on last line resolves issue but no good for me

    4) Checked but no missing references found.

    Now I have two questions:

    1) How do I resolve this error?

    2) How do I get a window for selecting print driver in case the driver is missing / is there any generic print driver which is present on all Windows Server OS.

    Below is the code:

    Thanks in advance.

    Const WbemAuthenticationLevelPktPrivacy = 6
    
    Private Sub CmdCreatePrinter_Click()
    
       Set objexcel = CreateObject("Excel.Application")
        introw = 2
        'On Error Resume Next
     'On Error GoTo Errorhandler
    'SETS 'LOAD DRIVER' PRIVILEGE.
    
     a = Sheet1.Range("a" & introw).Value
     
    strcomputer = Sheet1.Range("h9").Text
     'strNamespace = “ \ Root \ cimv2”
    strUser = TxtUserName.Text     'For Network computer
    strPassword = TxtPassword.Text          ' For Network Computer
    '  strComputer = "."                     'For Local Computer
      
    
    Set objwbemLocator = CreateObject("WbemScripting.SWbemLocator")
    
    Set objWMIService = objwbemLocator.ConnectServer _
        (strcomputer, "\root\cimv2", strUser, strPassword) 'For Network Computer
    objWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy ' For Network Computer
    
        objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True
     
    'fqdn = InputBox("Enter FQDN for host name", "Enter FQDN")
    If MsgBox("Print queues would be created on " & strcomputer & ". Do you want to proceed?", vbYesNo, "Create Print Queue") = vbNo Then Exit Sub
    
    
    'SETS PRINTER PORT.
    Do While Sheet1.Range("A" & introw).Text <> ""
    
       Set objNewport = objWMIService.Get _
            ("Win32_TCPIPPrinterPort").SpawnInstance_
     
        objNewport.Name = Sheet1.Range("a" & introw).Text
     
        objNewport.Protocol = 2
     
        objNewport.HostAddress = Sheet1.Range("e" & introw).Text
     
        objNewport.PortNumber = "515"
        
        objNewport.Queue = "lp"
     
        objNewport.SNMPEnabled = False
     
        objNewport.Put_
     
    'SETS PRINTER TO PORT.
    
     'For i = 1 To 2
     
        Set objPrinter = objWMIService.Get _
            ("Win32_Printer").SpawnInstance_
      '      If i = 1 Then
                    objPrinter.DriverName = "Xerox Global Print Driver PS"
                    objPrinter.portname = objNewport.Name
                    objPrinter.DeviceID = Sheet1.Range("a" & introw).Text
                    objPrinter.Network = True
                    objPrinter.ShareName = Sheet1.Range("a" & introw).Text
                    'objprinter.Secur
            'Else
             '       objPrinter.DriverName = "Xerox Global Print Driver PCL6"
              '      objPrinter.portname = Sheet1.Range("g" & introw).Text
               '     objPrinter.DeviceID = Sheet1.Range("h" & introw).Text
                '    objPrinter.ShareName = Sheet1.Range("h" & introw).Text
            'End If
        objPrinter.Location = Sheet1.Range("B" & introw).Value & ""
    '    objprinter.configuraTION.PrinterMode = sheet1.Range("B" & introw).Text
        objPrinter.Comment = Sheet1.Range("c" & introw).Text & ""
       objPrinter.Network = True
     objPrinter.Enablebidi = True ' Enabling Bidirectional print
    
     
        objPrinter.Put_
     'Next
     introw = introw + 1
     Loop
     MsgBox (introw - 1 & " Printers Created")
     'Sheet1.TxtUid.Text = ""
     'Sheet1.TxtPwd.Text = ""
        Exit Sub
    Errorhandler:
        MsgBox Err.Number + Err.Description, vbExclamation + vbOKOnly
    
    End Sub
    

    • Moved by Bill_Stewart Tuesday, November 7, 2017 10:09 PM This is not VBA forum
    Saturday, September 23, 2017 3:17 PM

All replies

  • This is NOT VBScript an it will not work as a VBScript. It is either VB or VBA or you copied it from somewhere with the misunderstanding that it was VBScript.

    If you are using VB or VBA then pot in a forum for those language systems.

    If you are trying to copy a script and run it I recommend not using VBS and use PowerShell.  PowerShell has commands to manage print queues.

    In all cases all forums will require you to post the complete error and not just the text.

    Please read this first: This forum is for scripting questions rather than script requests  
    Also find scripts here: http://gallery.technet.microsoft.com

    Learn PowerSHell: https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276


    \_(ツ)_/

    Saturday, September 23, 2017 7:16 PM
  • Thats right JVR its VBA. The error posted is only the complete error.
    Sunday, September 24, 2017 7:11 AM
  • No.  It is the error trapped by VBA and translated.  There is more information such as the line number and line text that caused the error.  Use the debugger to find the whole error.

    I recommend that you learn VBA and you will understand what I am asking.  Also this is NOT a VBA forum so please post in the VBA developers forum to get help using VBA.

    The documentation for VBA is on the Microsoft site and will explain how error handling works in VBA. 

    Most technical forums will not help you with code copied from the Internet.  You should ask the author of the code for help.


    \_(ツ)_/


    • Edited by jrv Sunday, September 24, 2017 9:30 AM
    Sunday, September 24, 2017 9:29 AM