如何使用vb脚本设置单选按钮?

时间:2016-10-08 19:57:30

标签: vbscript automation

我想在我的电脑上设定时间。为此,我编写了三个不同国家/地区的3个单选按钮,以便用户可以通过选择其中一个单选按钮来设置时区。

请在下面的错误中提供帮助:

Set objExecObject = objShell.Exec("tzutil.exe /s """ & TimeZoneFriendlyName & "") 
WScript.Echo "Setting the time zone sucessfully." 

Error message

2 个答案:

答案 0 :(得分:2)

<p><b>Search <u>w</u>here</b></p>
<p><INPUT Name=tb2 TYPE=RADIO CHECKED AccessKey=w> Google <INPUT Name=tb2 TYPE=RADIO> Wikipedia <INPUT Name=tb2 TYPE=RADIO> Nethack <INPUT Name=tb2 TYPE=RADIO> MSDN <INPUT Name=tb2 TYPE=RADIO> Australia <INPUT Name=tb2 TYPE=RADIO> Maps </p>

Sub Search
'   On Error Resume Next
    Set WshShell = CreateObject("WScript.Shell")
    If tb2(5).Checked=True then WshShell.Run "https://www.google.com/maps/place/" & Replace(tb1.value, " ", "+")
    If tb2(4).Checked=True then WshShell.Run "https://www.google.com/search?q=" & Replace(tb1.value, " ", "+") & "&cr=countryAU"
    If tb2(3).Checked=True then WshShell.Run "https://www.google.com/search?q=site:msdn.com+" & Replace(tb1.value, " ", "+")
    If tb2(2).Checked=True then WshShell.Run "https://www.google.com/search?q=nethack+" & Replace(tb1.value, " ", "+")
    If tb2(1).Checked=True then WshShell.Run "https://en.wikipedia.org/wiki/" & tb1.value
    If tb2(0).Checked=True then WshShell.Run "https://www.google.com/search?q=" & Replace(tb1.value, " ", "+")
    WshShell.RegWrite "HKCU\Software\StackOverflow\VBS\Searchterm", tb1.value & vbtab & WshShell.RegRead("HKCU\Software\StackOverflow\VBS\Searchterm")
    If tb3.checked=true then window.close
End Sub

你的报价也错了。 Cmd /k将允许您查看CMD中发生的情况,一旦工作就将其删除。

 WScript.CreateObject("WScript.Shell").Run "cmd /k tzutil.exe /s """ & TimeZoneFriendlyName & """"

这是一个HTA,显示单选按钮,复选框,按钮,并显示如何实现键盘访问。

<HTML> 
<HEAD><TITLE>Search</TITLE>
  <HTA:APPLICATION ID="oMyApp" 
    APPLICATIONNAME="Search" 
    BORDER="normal"
    BORDERSTYLE="sunken"
    CONTEXTMENU="yes"
    CAPTION="yes"
    ICON="C:\windows\system32\shell32,-5"
    SCROLL="no"
    SHOWINTASKBAR="yes"
    SINGLEINSTANCE="yes"
    SYSMENU="yes"
    WINDOWSTATE="normal"
  />

<style>
BODY  {font-size :100%;font-family: Arial, Helvetica, sans-serif;color: black;
  background:URL(images/watermark.gif);background-color: white;
  margin-top:20pt; margin-left:40pt; margin-right:10pt ; text-align:Justify}
P  {margin-left:0pt;margin-right:0pt}
</style>



<SCRIPT LANGUAGE="VBScript">
Dim Searchterm

Sub Search
'   On Error Resume Next
    Set WshShell = CreateObject("WScript.Shell")
    If tb2(5).Checked=True then WshShell.Run "https://www.google.com/maps/place/" & Replace(tb1.value, " ", "+")
    If tb2(4).Checked=True then WshShell.Run "https://www.google.com/search?q=" & Replace(tb1.value, " ", "+") & "&cr=countryAU"
    If tb2(3).Checked=True then WshShell.Run "https://www.google.com/search?q=site:msdn.com+" & Replace(tb1.value, " ", "+")
    If tb2(2).Checked=True then WshShell.Run "https://www.google.com/search?q=nethack+" & Replace(tb1.value, " ", "+")
    If tb2(1).Checked=True then WshShell.Run "https://en.wikipedia.org/wiki/" & tb1.value
    If tb2(0).Checked=True then WshShell.Run "https://www.google.com/search?q=" & Replace(tb1.value, " ", "+")
    WshShell.RegWrite "HKCU\Software\StackOverflow\VBS\Searchterm", tb1.value & vbtab & WshShell.RegRead("HKCU\Software\StackOverflow\VBS\Searchterm")
    If tb3.checked=true then window.close
End Sub

Sub Init
    Count = 0
    window.resizeTo 550, 400
'   On Error Resume Next
    Set WshShell = CreateObject("WScript.Shell")
    HistoryArr = Split(WshShell.RegRead("HKCU\Software\StackOverflow\VBS\Searchterm"), Chr(9))
    tb1.value = HistoryArr(0)
    For each t in HistoryArr
        Count=Count + 1
        Set Hist = document.createElement("OPTION")
        Hist.Text=t
        Hist.Value=Count
        HistoryList.Add(Hist)
        Count=Count + 1
        If Count > 50 then exit For
    next
    tb1.select
'   tb1.focus
End Sub

Sub Paste
    tb1.value=document.parentwindow.clipboardData.GetData("TEXT")
    tb1.select
End Sub

Sub LBTransfer
    x = CLng(HistoryList.Options.selectedindex)
    tb1.value = HistoryList.Options(x).text
    tb1.select
End Sub

Sub LBTransferK
    x = CLng(HistoryList.Options.selectedindex)
    tb1.value = HistoryList.Options(x).text
    HistoryList.focus
End Sub
</script>
</head>

<body Onload=Init>
<p><b>Enter <u>s</u>earch term</b></p>
<p><INPUT Name=tb1 TYPE=Text size=60 AccessKey=s> <INPUT Type=Button onclick=paste Value=Paste AccessKey=p></p> 
<p><b><u>H</u>istory</b></p>
<p><SELECT NAME="HistoryList" SIZE="4" Onclick=LBTransfer OnKeyPress=LBTransferK AccessKey=h>
</SELECT></p>
<p><b>Search <u>w</u>here</b></p>
<p><INPUT Name=tb2 TYPE=RADIO CHECKED AccessKey=w> Google <INPUT Name=tb2 TYPE=RADIO> Wikipedia <INPUT Name=tb2 TYPE=RADIO> Nethack <INPUT Name=tb2 TYPE=RADIO> MSDN <INPUT Name=tb2 TYPE=RADIO> Australia <INPUT Name=tb2 TYPE=RADIO> Maps </p>
<p><INPUT NAME="Search" TYPE="SUBMIT" VALUE="Search" OnClick=Search> <INPUT Name=tb3 TYPE=CHECKBOX CHECKED AccessKey=c> <u>C</u>lose on search</p>


</body>
</html>

答案 1 :(得分:0)

您无法使用VBscript创建单选按钮。您需要使用HTA或其他包装器。

错误似乎是您没有设置shell对象的问题

所以要使用

Set objExecObject = objShell.Exec("tzutil.exe /s """ & TimeZoneFriendlyName & "") 
WScript.Echo "Setting the time zone sucessfully." 

你首先要有一个

Set objShell = CreateObject("Wscript.Shell")

之前