Link to home
Start Free TrialLog in
Avatar of resourcepc
resourcepc

asked on

Need vbscript to check if printer exists

I have the following code that I'm using in an HTML file so that users can add, remove, and even set their default printer.  I have a function called "setDefault" that is called when a user clicks a button called "set as default" and it runs the following code:

Function setDefault(pname)
On Error Resume Next
  Set WshNetwork = CreateObject("WScript.Network")
  Set objShell = CreateObject("WScript.Shell")
 
  strSessionName = objShell.ExpandEnvironmentStrings("%SESSIONNAME%")
If strSessionName <> "" And LCase(strSessionName) <> "console" Then
msgbox ("You cannot set default printers on Citrix")
Else
  mkDef = msgbox ("Do you want " & pname & " to be your default printer?", 292, "Default Printer Status")
  If mkDef = 6 Then
     WshNetwork.SetDefaultPrinter pname
     msgbox ("Printer " & pname & " has been set as default successfully.")
  Else
  End If
  End If
End Function

The problem I'm having is if they don't have the printer installed, I want it to pop up and say that this printer does not exist.
Avatar of callrs
callrs

http://www.aspfaq.com/show.asp?id=2396 #2396 : How do I determine if a VBScript-based object exists?
http://www.tech-archive.net/Archive/Scripting/microsoft.public.scripting.vbscript/2004-07/0066.html microsoft.public.scripting.vbscript: Re: VBScript to check if printer exists if so delete it

Second link states:
"You should be able to suppress your error situation with the following
statement (put it just above the command that might fail):
   On Error Resume Next
You can "cancel" the error suppressing afterward if you want with
    On Error Goto 0
Avatar of resourcepc

ASKER

I don't want the script to continue if there's an error.  I want it to present a message box to the user saying "This printer is not installed on your computer"
This is one way to do it
---------------------------------
Function setDefault(pname)
    On Error Resume Next
    Set WshNetwork = CreateObject("WScript.Network")
    Set objShell = CreateObject("WScript.Shell")
   
    strSessionName = objShell.ExpandEnvironmentStrings("%SESSIONNAME%")
    If strSessionName <> "" And LCase(strSessionName) <> "console" Then
        MsgBox ("You cannot set default printers on Citrix")
    Else
        mkDef = MsgBox("Do you want " & pname & " to be your default printer?", 292, "Default Printer Status")
        If mkDef = 6 Then
            WshNetwork.SetDefaultPrinter pname
            If Err Then
                MsgBox Err.Description, vbCritical
                Err.Clear
            Else
                MsgBox ("Printer " & pname & " has been set as default successfully.")
            End If
        Else
        End If
    End If
End Function
If you want to have a ErrorHandler part in the function you can also do like this.

------------------------------------------------------
Function setDefault(pname)
On Error GoTo ErrorHandler

    Set WshNetwork = CreateObject("WScript.Network")
    Set objShell = CreateObject("WScript.Shell")
   
    strSessionName = objShell.ExpandEnvironmentStrings("%SESSIONNAME%")
    If strSessionName <> "" And LCase(strSessionName) <> "console" Then
        MsgBox ("You cannot set default printers on Citrix")
    Else
        mkDef = MsgBox("Do you want " & pname & " to be your default printer?", 292, "Default Printer Status")
        If mkDef = 6 Then
            WshNetwork.SetDefaultPrinter pname
            MsgBox ("Printer " & pname & " has been set as default successfully.")
        End If
    End If
   
    Exit Function
   
ErrorHandler:
    MsgBox Err.Description, vbCritical

End Function

Use the following and that should get you the solution you want..

We initially will be checking whether the passed Printer name matches any of our installed printers if not then the msgbox will be popuped...!! Else the rest of the stuff you want to do.

============================================================
Function setDefault(pname)
On Error GoTo ErrorHandler

    Set WshNetwork = CreateObject("WScript.Network")
    Set objShell = CreateObject("WScript.Shell")
    flgFoundPrinter=0
    Set Printers = WshNetwork.EnumPrinterConnections

   For i = 0 to Printers.Count - 1 Step 2
      if Printers.Item(i+1)=pname then
            'FOUND THE REQUESTED PRINTER
            flgFoundPrinter=1
            EXIT FOR
      end if
   Next    
if flgFoundPrinter=1 then
    strSessionName = objShell.ExpandEnvironmentStrings("%SESSIONNAME%")
    If strSessionName <> "" And LCase(strSessionName) <> "console" Then
        MsgBox ("You cannot set default printers on Citrix")
    Else
        mkDef = MsgBox("Do you want " & pname & " to be your default printer?", 292, "Default Printer Status")
        If mkDef = 6 Then
            WshNetwork.SetDefaultPrinter pname
            MsgBox ("Printer " & pname & " has been set as default successfully.")
        End If
    End If
else
 MsgBox("This printer is not installed on your computer")
end if    
    Exit Function
   
ErrorHandler:
    MsgBox Err.Description, vbCritical

End Function

============================================================


- NISHANT SHETTY


I read that you can't use an ErrorHandler in VBScript, and I've tested this and it doesn't work.  
If there was a way to determine if a registry value existed, I could do it that way.  

HKCU\Software\Microsoft\WindowsNT\CurrentVersion\Devices\PrinterName

I know how to see if registry keys exists, but not values.  Anyone know?
My code:

<html>
<head>
<title>Printer Management Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<SCRIPT LANGUAGE="VBScript">

Function delPrint(pname)
  Set objNetwork = CreateObject("WScript.Network")
  Set objShell = CreateObject("WScript.Shell")
  strSessionName = objShell.ExpandEnvironmentStrings("%SESSIONNAME%")
If strSessionName <> "" And LCase(strSessionName) <> "console" Then
msgbox ("You cannot delete printers on Citrix")
Else
  confDel = msgbox ("Do you want to remove " & pname & " ?",vbYesNo)
  If confDel = 6 Then
     objNetwork.RemovePrinterConnection pname
     msgbox (pname & " has been removed.")
  Else
     msgbox (pname & " will not be removed.")
  End If
  End If
End Function


Function addPrint(pname)
  Set WshNetwork = CreateObject("WScript.Network")
  Set objShell = CreateObject("WScript.Shell")
  strSessionName = objShell.ExpandEnvironmentStrings("%SESSIONNAME%")
If strSessionName <> "" And LCase(strSessionName) <> "console" Then
msgbox ("You cannot add printers on Citrix")
Else
  WshNetwork.AddWindowsPrinterConnection pname
 
  mkDef = msgbox ("Do you want " & pname & " to be your default printer?", 292, "Default Printer Status")
  If mkDef = 6 Then
     WshNetwork.SetDefaultPrinter pname
     msgbox ("Printer " & pname & " installed as default successfully.")
  Else
     msgbox ("Printer " & pname & " installed successfully.")
  End If  
  End If
End Function

Function addMktg(pname)
  Set WshNetwork = CreateObject("WScript.Network")
  Set objShell = CreateObject("WScript.Shell")
  Set ADSysInfo = CreateObject("ADSystemInfo")
  Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
  strGroups = LCase(Join(CurrentUser.MemberOf))
  strSessionName = objShell.ExpandEnvironmentStrings("%SESSIONNAME%")
If strSessionName <> "" And LCase(strSessionName) <> "console" Then
msgbox ("You cannot add printers on Citrix")
Else
If Instr(strGroups, "cn=marketing") Then
  WshNetwork.AddWindowsPrinterConnection pname
 
  mkDef = msgbox ("Do you want " & pname & " to be your default printer?", 292, "Default Printer Status")
  If mkDef = 6 Then
     WshNetwork.SetDefaultPrinter pname
     msgbox ("Printer " & pname & " installed as default successfully.")
  Else
     msgbox ("Printer " & pname & " installed successfully.")
  End If
  Else
     msgbox ("Only the Marketing department can add this printer")
  End If  
  End If
End Function

Function addHR(pname)
  Set WshNetwork = CreateObject("WScript.Network")
  Set objShell = CreateObject("WScript.Shell")
  Set ADSysInfo = CreateObject("ADSystemInfo")
  Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
  strGroups = LCase(Join(CurrentUser.MemberOf))
  strSessionName = objShell.ExpandEnvironmentStrings("%SESSIONNAME%")
If strSessionName <> "" And LCase(strSessionName) <> "console" Then
msgbox ("You cannot add printers on Citrix")
Else
If Instr(strGroups, "cn=human resources") Then
  WshNetwork.AddWindowsPrinterConnection pname
 
  mkDef = msgbox ("Do you want " & pname & " to be your default printer?", 292, "Default Printer Status")
  If mkDef = 6 Then
     WshNetwork.SetDefaultPrinter pname
     msgbox ("Printer " & pname & " installed as default successfully.")
  Else
     msgbox ("Printer " & pname & " installed successfully.")
  End If
  Else
     msgbox ("Only the Human Resources department can add this printer")
  End If  
  End If
End Function


Function setDefault(pname)
  Set WshNetwork = CreateObject("WScript.Network")
  Set objShell = CreateObject("WScript.Shell")
  Const HKEY_CURRENT_USER = &H80000001
  strPrinter = pname
  strSessionName = objShell.ExpandEnvironmentStrings("%SESSIONNAME%")
If strSessionName <> "" And LCase(strSessionName) <> "console" Then
msgbox ("You cannot set default printers on Citrix")
Else
  mkDef = msgbox ("Do you want " & pname & " to be your default printer?", 292, "Default Printer Status")
  If mkDef = 6 Then
     WshNetwork.SetDefaultPrinter pname
     msgbox ("Printer " & pname & " has been set as default successfully.")
  Else
  End If
  End If
End Function

</SCRIPT>
<body>
<p>&nbsp;</p>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="1">
  <tr>
    <td valign="top" bgcolor="#999999">
      <table width="600" border="0" cellspacing="1" cellpadding="0">
        <tr bgcolor="#CCCCCC">
          <td colspan="5">
                  <table width="600" border="0" cellspacing="0" cellpadding="0" bgcolor="#008000">
              <tr>
                <td width="147"><strong>
                        <font size="2" face="helvetica, verdana, arial" color="#FFFFFF">&nbsp;Location</font></strong></td>
                <td width="283"><strong>
                        <font size="2" face="helvetica, verdana, arial" color="#FFFFFF"> &nbsp;Printer</font></strong></td>
                <td width="70"><strong><font size="1" face="helvetica, verdana, arial">&nbsp;</font></strong></td>
                <td width="100">&nbsp;</td>
              </tr>
            </table>
          </td>
        </tr>
        <tr bgcolor="#ffffff">
          <td width="146">
                  <font face="Verdana, Arial, Helvetica, sans-serif" size="1">&nbsp;1st
                  Floor Mktg Dept</font></td>
          <td width="180">
                  <font face="Verdana, Arial, Helvetica, sans-serif" size="1">&nbsp;Idaho</font></td>
          <td width="70">
            <div align="center"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
              <input type="button" name="Button23" value="Add" style="width:40; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:8pt; color:#000000; background-color:#E8E8E8;" onClick=addMktg('\\thevault\idaho')>
              </font></div>
          </td>
          <td width="100"><div align="center"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
              <input type="button" name="Button223" value="Remove" style="width:70; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:8pt; color:#000000; background-color:#E8E8E8;" onClick=delPrint('\\thevault\idaho')>
              </font></div>
          </td>
          <td width="100"><div align="center"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
              <input type="button" name="Button24" value="Set Default" style="width:70; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:8pt; color:#000000; background-color:#E8E8E8;" onClick=setDefault('\\thevault\idaho')>
              </font></div>
          </td>

        </tr>

If the user doesn't have the "\\thevault\idaho" printer installed on their workstation, I want to send a msbox saying that.
I figured it out using code from this site:
http://www.computerperformance.co.uk/ezine/ezine61.htm#This%20weeks%20mission%20-%20master%20pwdLastSet

This is how I did it:

Function setDefault(pname)
  Set WshNetwork = CreateObject("WScript.Network")
  Set objShell = CreateObject("WScript.Shell")
  On Error Resume Next
  strSessionName = objShell.ExpandEnvironmentStrings("%SESSIONNAME%")
If strSessionName <> "" And LCase(strSessionName) <> "console" Then
msgbox ("You cannot set default printers on Citrix")
Else
  mkDef = msgbox ("Do you want " & pname & " to be your default printer?", 292, "Default Printer Status")
  If mkDef = 6 Then
      WshNetwork.SetDefaultPrinter pname
      If err.number = -2147352567 Then
      msgbox ("You do not have this printer installed")
      Else
     msgbox ("Printer " & pname & " has been set as default successfully.")
  End If
  End If
  End If
End Function

This prevents a user from doing anything on a Citrix server, and it'll let them know if that printer is not installed.
Fine that would also work, Actually the code I pasted even if u remove that 'On Error GoTo ErrorHandler' line, the code will still work.

Anyways that u have found a solution though bit different from what should be if its working and ur fine then thats the solution.

Ensure that ur code works on different versions also, as sometimes the errorcode are changed in different versions.

- nISHANT

ASKER CERTIFIED SOLUTION
Avatar of DarthMod
DarthMod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial