0

我有一个带有一点 VBScript 的页面,当单击按钮时会启动打印机安装。脚本成功完成后会出现警报。

我现在正在尝试添加另一个按钮单击事件(可能使用 jQuery),它通知用户打印机安装已经开始(在按钮单击时)。这是因为安装打印机可能需要 20 秒到 1 分钟不等的时间。

实现以下代码时,我收到一个错误,即在 jQuery 代码块中需要一个对象

   <script type="text/vbscript">
        function AddP(pName)
            Set WshNetwork = CreateObject("Wscript.Network")
            WshNetwork.AddWindowsPrinterConnection pName
            MsgBox "Printer Added"
        end function
   </script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
        $(document).ready( function (){ 

            $("#btn").click( function(){

                 alert("Printer Install Started");

            });

         });
    </script>

 <td><a href="#" language="vbscript" onclick="AddP('\\PrinterName')"><input id="btn" name="Button1" type="button" value="Add"></a></td>
4

3 回答 3

1

如果您想添加错误检查并提示设置为默认值。这是我第一次真正地编写脚本,所以请记住这一点。

<script type="text/vbscript">
function AddP(pName)

Shortname = split(pName, "\")

    If MsgBox("Click YES to install  " &ShortName(3),VBYesNo, "Printer Utility") = vbYes Then

        MsgBox "Installing Printer " &ShortName(3) &vbCrLf &vbCrLf &"This may take a few moments"


        Set WshNetwork = CreateObject("Wscript.Network")

        On Error Resume Next
        WshNetwork.AddWindowsPrinterConnection pName
            If Err.Number <> 0 Then

            MsgBox "Printer is not available for install." &vbCrLF &"Please contact Tech Support for assistance"
                Else If MsgBox ("Would you Like  " &ShortName(3) &" to be your Default Printer?" ,vbyesno, "Printer Utility")  = vbYes Then

                WshNetwork.SetDefaultPrinter pName
                MSgBox ShortName(3) &"  Installed and Set as Default",0,"Printer Utility"
                Else MSgBox ShortName(3) &"  Installed",0,"Printer Utility"

                End if

            End If

        On Error GoTo 0
     Else Msgbox "Printer Install Cancelled"
    End if
end function

</脚本>

于 2013-05-10T21:59:01.140 回答
0

如果您想在添加打印机时给用户一些指示,您可以更改按钮状态。

<script type="text/vbscript">
   function AddP(pName)
       Dim allButton1s
       Set allButton1s = document.getElementsByName("Button1")
       allButton1s(0).value = "Please wait..."
       Set WshNetwork = CreateObject("Wscript.Network")
       WshNetwork.AddWindowsPrinterConnection pName
       MsgBox "Printer Added"
       allButton1s(0).value = "Add"
   end function
</script>
于 2012-11-28T23:51:19.223 回答
0

如果真的只是想在启动打印机安装之前发出警报,请在它之前添加另一个 MsgBox 行

<script type="text/vbscript">
   function AddP(pName)
       MsgBox "Printer Install Started"
       Set WshNetwork = CreateObject("Wscript.Network")
       WshNetwork.AddWindowsPrinterConnection pName
       MsgBox "Printer Added"
   end function
</script>

注意:打印机安装不会开始,直到用户点击“打印安装开始”警报上的确定,如果您想避免使用按钮状态更改答案来代替。

于 2012-11-28T23:52:48.737 回答