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.Netw ork")
Set objShell = CreateObject("WScript.Shel l")
strSessionName = objShell.ExpandEnvironment Strings("% SESSIONNAM E%")
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.SetDefaultPrint er 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.
Function setDefault(pname)
On Error Resume Next
Set WshNetwork = CreateObject("WScript.Netw
Set objShell = CreateObject("WScript.Shel
strSessionName = objShell.ExpandEnvironment
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.SetDefaultPrint
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.
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.Netw ork")
Set objShell = CreateObject("WScript.Shel l")
strSessionName = objShell.ExpandEnvironment Strings("% SESSIONNAM E%")
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.SetDefaultPrint er 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
--------------------------
Function setDefault(pname)
On Error Resume Next
Set WshNetwork = CreateObject("WScript.Netw
Set objShell = CreateObject("WScript.Shel
strSessionName = objShell.ExpandEnvironment
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.SetDefaultPrint
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.Netw ork")
Set objShell = CreateObject("WScript.Shel l")
strSessionName = objShell.ExpandEnvironment Strings("% SESSIONNAM E%")
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.SetDefaultPrint er pname
MsgBox ("Printer " & pname & " has been set as default successfully.")
End If
End If
Exit Function
ErrorHandler:
MsgBox Err.Description, vbCritical
End Function
--------------------------
Function setDefault(pname)
On Error GoTo ErrorHandler
Set WshNetwork = CreateObject("WScript.Netw
Set objShell = CreateObject("WScript.Shel
strSessionName = objShell.ExpandEnvironment
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.SetDefaultPrint
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.Netw
Set objShell = CreateObject("WScript.Shel
flgFoundPrinter=0
Set Printers = WshNetwork.EnumPrinterConn
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.ExpandEnvironment
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.SetDefaultPrint
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
ASKER
I read that you can't use an ErrorHandler in VBScript, and I've tested this and it doesn't work.
ASKER
If there was a way to determine if a registry value existed, I could do it that way.
HKCU\Software\Microsoft\Wi ndowsNT\Cu rrentVersi on\Devices \PrinterNa me
I know how to see if registry keys exists, but not values. Anyone know?
HKCU\Software\Microsoft\Wi
I know how to see if registry keys exists, but not values. Anyone know?
ASKER
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.Netw ork")
Set objShell = CreateObject("WScript.Shel l")
strSessionName = objShell.ExpandEnvironment Strings("% SESSIONNAM E%")
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.RemovePrinterCo nnection 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.Netw ork")
Set objShell = CreateObject("WScript.Shel l")
strSessionName = objShell.ExpandEnvironment Strings("% SESSIONNAM E%")
If strSessionName <> "" And LCase(strSessionName) <> "console" Then
msgbox ("You cannot add printers on Citrix")
Else
WshNetwork.AddWindowsPrint erConnecti on pname
mkDef = msgbox ("Do you want " & pname & " to be your default printer?", 292, "Default Printer Status")
If mkDef = 6 Then
WshNetwork.SetDefaultPrint er 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.Netw ork")
Set objShell = CreateObject("WScript.Shel l")
Set ADSysInfo = CreateObject("ADSystemInfo ")
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
strGroups = LCase(Join(CurrentUser.Mem berOf))
strSessionName = objShell.ExpandEnvironment Strings("% SESSIONNAM E%")
If strSessionName <> "" And LCase(strSessionName) <> "console" Then
msgbox ("You cannot add printers on Citrix")
Else
If Instr(strGroups, "cn=marketing") Then
WshNetwork.AddWindowsPrint erConnecti on pname
mkDef = msgbox ("Do you want " & pname & " to be your default printer?", 292, "Default Printer Status")
If mkDef = 6 Then
WshNetwork.SetDefaultPrint er 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.Netw ork")
Set objShell = CreateObject("WScript.Shel l")
Set ADSysInfo = CreateObject("ADSystemInfo ")
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
strGroups = LCase(Join(CurrentUser.Mem berOf))
strSessionName = objShell.ExpandEnvironment Strings("% SESSIONNAM E%")
If strSessionName <> "" And LCase(strSessionName) <> "console" Then
msgbox ("You cannot add printers on Citrix")
Else
If Instr(strGroups, "cn=human resources") Then
WshNetwork.AddWindowsPrint erConnecti on pname
mkDef = msgbox ("Do you want " & pname & " to be your default printer?", 292, "Default Printer Status")
If mkDef = 6 Then
WshNetwork.SetDefaultPrint er 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.Netw ork")
Set objShell = CreateObject("WScript.Shel l")
Const HKEY_CURRENT_USER = &H80000001
strPrinter = pname
strSessionName = objShell.ExpandEnvironment Strings("% SESSIONNAM E%")
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.SetDefaultPrint er pname
msgbox ("Printer " & pname & " has been set as default successfully.")
Else
End If
End If
End Function
</SCRIPT>
<body>
<p> </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"> Loca tion</font ></strong> </td>
<td width="283"><strong>
<font size="2" face="helvetica, verdana, arial" color="#FFFFFF"> Printer</font></stro ng></td>
<td width="70"><strong><font size="1" face="helvetica, verdana, arial"> </font></stro ng></td>
<td width="100"> </td>
</tr>
</table>
</td>
</tr>
<tr bgcolor="#ffffff">
<td width="146">
<font face="Verdana, Arial, Helvetica, sans-serif" size="1"> 1st
Floor Mktg Dept</font></td>
<td width="180">
<font face="Verdana, Arial, Helvetica, sans-serif" size="1"> 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('\\thevaul t\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('\\thevau lt\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('\\thev ault\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.
<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.Netw
Set objShell = CreateObject("WScript.Shel
strSessionName = objShell.ExpandEnvironment
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.RemovePrinterCo
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.Netw
Set objShell = CreateObject("WScript.Shel
strSessionName = objShell.ExpandEnvironment
If strSessionName <> "" And LCase(strSessionName) <> "console" Then
msgbox ("You cannot add printers on Citrix")
Else
WshNetwork.AddWindowsPrint
mkDef = msgbox ("Do you want " & pname & " to be your default printer?", 292, "Default Printer Status")
If mkDef = 6 Then
WshNetwork.SetDefaultPrint
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.Netw
Set objShell = CreateObject("WScript.Shel
Set ADSysInfo = CreateObject("ADSystemInfo
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
strGroups = LCase(Join(CurrentUser.Mem
strSessionName = objShell.ExpandEnvironment
If strSessionName <> "" And LCase(strSessionName) <> "console" Then
msgbox ("You cannot add printers on Citrix")
Else
If Instr(strGroups, "cn=marketing") Then
WshNetwork.AddWindowsPrint
mkDef = msgbox ("Do you want " & pname & " to be your default printer?", 292, "Default Printer Status")
If mkDef = 6 Then
WshNetwork.SetDefaultPrint
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.Netw
Set objShell = CreateObject("WScript.Shel
Set ADSysInfo = CreateObject("ADSystemInfo
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
strGroups = LCase(Join(CurrentUser.Mem
strSessionName = objShell.ExpandEnvironment
If strSessionName <> "" And LCase(strSessionName) <> "console" Then
msgbox ("You cannot add printers on Citrix")
Else
If Instr(strGroups, "cn=human resources") Then
WshNetwork.AddWindowsPrint
mkDef = msgbox ("Do you want " & pname & " to be your default printer?", 292, "Default Printer Status")
If mkDef = 6 Then
WshNetwork.SetDefaultPrint
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.Netw
Set objShell = CreateObject("WScript.Shel
Const HKEY_CURRENT_USER = &H80000001
strPrinter = pname
strSessionName = objShell.ExpandEnvironment
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.SetDefaultPrint
msgbox ("Printer " & pname & " has been set as default successfully.")
Else
End If
End If
End Function
</SCRIPT>
<body>
<p> </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"> Loca
<td width="283"><strong>
<font size="2" face="helvetica, verdana, arial" color="#FFFFFF"> Printer</font></stro
<td width="70"><strong><font size="1" face="helvetica, verdana, arial"> </font></stro
<td width="100"> </td>
</tr>
</table>
</td>
</tr>
<tr bgcolor="#ffffff">
<td width="146">
<font face="Verdana, Arial, Helvetica, sans-serif" size="1"> 1st
Floor Mktg Dept</font></td>
<td width="180">
<font face="Verdana, Arial, Helvetica, sans-serif" size="1"> Idaho</font
<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;"
</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;"
</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;"
</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.
ASKER
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.Netw ork")
Set objShell = CreateObject("WScript.Shel l")
On Error Resume Next
strSessionName = objShell.ExpandEnvironment Strings("% SESSIONNAM E%")
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.SetDefaultPrint er 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.
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.Netw
Set objShell = CreateObject("WScript.Shel
On Error Resume Next
strSessionName = objShell.ExpandEnvironment
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.SetDefaultPrint
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.tech-archive.net/Archive/Scripting/microsoft.public.scripting.vbscript/2004-07/0066.html microsoft.public.scripting
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