【问题标题】:HTA with sub and a variable wont show in another sub带有子和变量的 HTA 不会显示在另一个子中
【发布时间】:2023-05-02 09:13:02
【问题描述】:

我一直在研究这个......但无法弄清楚它为什么会起作用

我有一个下拉列表,选择它设置它设置一个值,这是它的工作...

但是当我希望该值显示在另一个子例程中时它不会显示?

请帮帮我?

如果您喜欢任何代码应该以某种方式编写,请告诉我...我是从互联网上学习的,所以我没有正确“训练”...

<html>
<head>
<title>Break Logs</title>

<HTA:APPLICATION

icon=icon.ico
    singleinstance="yes"
border="thin"
borderStyle="normal"
caption="yes"
maximizeButton="no"
minimizeButton="no"
showInTaskbar="yes"
windowState="normal"
innerBorder="yes"
navigable="no"
scroll="no"
scrollFlat="no"
sysMenu="yes"
>

</head>

<SCRIPT Language="VBScript">

Window.ResizeTo 250, 250

Dim objFSO, wshShell, strUser, Name, LastName, strLogs, strLogFile, strLocation


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'       GETTING USERS NAME FROM AD  THIS IM NOT USING WHEN AT HOME TESTING...
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
sub asdf

Set wshShell = CreateObject("WScript.Shell")
strUser = wshShell.ExpandEnvironmentStrings("%USERNAME%")
Const ADS_SCOPE_SUBTREE = 2
Set objRootDSE = GetObject("LDAP://RootDSE")
strDomain = objRootDSE.Get("DefaultNamingContext")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand = CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = "SELECT distinguishedName FROM 'LDAP://" & strDomain & "'                 WHERE objectCategory='User' AND samAccountName = '" & strUser & "'"
Set objRecordSet = objCommand.Execute
If Not objRecordSet.EOF Then
strDN = objRecordSet.Fields("distinguishedName").Value
End If
Set MyUser = GetObject ("LDAP://" & strDN)
Name = myUser.GivenName
LastName = myUser.sn

end sub

Name = "Pavle"
LastName = "Stojanovic"

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'       SETTING TITLEBAR NAME
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
document.title = "Break Logs For " & Name & " " & LastName

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
CONST ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")

strLocation = "F:\"
strLogFile = "Late Breaks.txt"

If Not objFSO.FileExists(strLocation & strLogFile) Then
   objFSO.CreateTextFile(strLocation & strLogFile)
End If



Sub Breaks

MorningBreak = (" - Morning Break " & Name & " was late: " & _ 
        HowLate.Value & " Minutes" & vbCrLf & _
    " - Reason: " & Reason.Value & vbCrLF & _
    " - Date Entered: " & Now())


LunchBreak = (" - Lunch Break " & Name & " was late: " & _ 
        HowLate.Value & " Minutes" & vbCrLf & _
    " - Reason: " & Reason.Value & vbCrLF & _
    " - Date Entered: " & Now())

AfternoonBreak = (" - Afternoon Break " & Name & " was late: " & _ 
        HowLate.Value & " Minutes" & vbCrLf & _
    " - Reason: " & Reason.Value & vbCrLF & _
    " - Date Entered: " & Now())

RNA = (" - Ring No Answer Logged By " & Name & vbCrLF & _
   " - On Date: " & Now() & vbCrLF & _ 
   " - Reason: " & Reason.Value)


Other = (" - Date Entered: " & Now() & vbCrLf & _ 
 " - Reason: " & Reason.Value)



If DropDown.Value = 1 Then

' Nothing to be done in this option...

ElseIf DropDown.Value = 2 Then

BreakCode = MorningBreak

ElseIf DropDown.Value = 3 Then

BreakCode = LunchBreak

ElseIf DropDown.Value = 4 Then

BreakCode = AfternoonBreak

ElseIf DropDown.Value = 5 Then

BreakCode = RNA

ElseIf DropDown.Value = 6 Then

BreakCode = Other

Else

MsgBox "Something went wrong, this option shouldn't popup"

End If


' If I do MsgBox to display BreakCode it works here before leaving the sub ?
End Sub



Sub Submit

MsgBox BreakCode ' this doesnt show up and below wont write to text ???

Set strLogs = objFSO.OpenTextFile(strLocation & strLogFile, ForAppending, True)
    strLogs.WriteLine BreakCode & vbCrLf
     strLogs.Close

End Sub



</SCRIPT>
<body bgColor="LightSteelBlue">

<select size="1" name="DropDown" onChange="Breaks">
 <option value="1">Choose Break</option>
  <option value="2">Morning Break</option>
    <option value="3">Lunch Break</option>
     <option value="4">Afternoon Break</option>
      <option value="5">Ring No Answer</option>
       <option value="6">Other</option>
        </select>

<BR>
<BR>

Reason:
<BR>
<input type="text" name="Reason" size="10" style="width:200">

<BR>
<BR>

Minutes Late:
<BR>
<input type="text" name="HowLate" size="10"> 

<BR>
<BR>
<input type="button" value="Submit" onClick="Submit" title="Added Reason for being late.">

</body>
</html>

【问题讨论】:

    标签: vbscript hta


    【解决方案1】:

    @Ansgar 在此代码中发现 一个 错误显然值得称赞 (+1)。如果将其与干净的“解决方案”进行比较:

    HTA:

    <html>
     <head>
      <title>Break Logs</title>
      <hta:application
       id="breaks"
       scroll="no"
      ></hta>
      <script type="text/vbscript" src="breaks.vbs"></script>
     </head>
     <body bgColor="LightSteelBlue">
      <form>
       <select size="1" name="DropDown">
        <option value="1">Choose Break</option>
        <option value="2">Morning Break</option>
        <option value="3">Lunch Break</option>
        <option value="4">Afternoon Break</option>
        <option value="5">Ring No Answer</option>
        <option value="6">Other</option>
       </select>
       <br />
       <br />
       Reason:
       <br />
       <input type="text" name="Reason" size="10" style="width:200">
       <br />
       <br />
       Minutes Late:
       <br />
       <input type="text" name="HowLate" size="10">
       <br />
       <br />
       <input type="button" value="Save" onClick="SaveIt" title="Added Reason for being late.">
      </form>
     </body>
    </html>
    

    VBS:

    Option Explicit
    
    Const ForAppending = 8
    Const csFSpec      = "wherever"
    
    Window.ResizeTo 250, 250
    
    Dim sFstName : sFstName = "Pavle"
    Dim sLstName : sLstName = "Stojanovic"
    document.title = "Break Logs For " & sFstName & " " & sLstName
    
    Sub SaveIt
      Dim nOpt : nOpt = CInt(document.all.DropDown.Value)
      If nOpt <> 1 Then
         Dim sReason  : sReason  = Trim(document.all.Reason.Value)
         Dim sHowLate : sHowLate = Trim(document.all.HowLate.Value)
         Dim sBreak   : sBreak   = document.all.DropDown.options(nOpt - 1).text
         If sReason <> "" And sHowLate <> "" Then
            Dim sLog   : sLog       = Join(Array( _
                  "- " & sBreak & " " & sFstName & " was late" _
                , "- Minutes:      " & sHowLate _
                , "- Reason:       " & sReason _
                , "- Date Entered: " & Now() _
             ), vbCrLf)
            MsgBox sLog
            Dim oFS    : Set oFS    = CreateObject("Scripting.FileSystemObject")
            Dim tsLogs : Set tsLogs = oFS.OpenTextFile(csFSpec, ForAppending, True)
            tsLogs.WriteLine sLog
            tsLogs.Close
         Else
            MsgBox "Fill in Reason and HowLate!"
         End If
      Else
         MsgBox "Choose break type!"
      End If
    End Sub
    

    你可以轻松拿起其他人:

    1. 糟糕的 HTML:介于 和 之间,没有(你应该使用像 tidy 这样的验证器)
    2. 提交表示提交到服务器;这对 .HTA 毫无意义
    3. 当 DropDown 选择机会时,选择 Reason 和 HowLate 是没有用的(通常用户稍后会填写它们)。实际上,DopDown 可以很好地自行处理选择更改
    4. 当用户点击Save 时,所有有趣的事情都会发生。所以根本不需要全局变量
    5. OpenTextFile(,,True) 将创建一个不存在的文件,因此检查日志文件的存在是不必要的

    你可以专注于中心缺陷:

    1. 您不能在生产环境中使用它,因为一旦您拥有多个用户,他们就会同时按下Save 并破坏日志文件。

    【讨论】:

      【解决方案2】:

      您没有在任何地方显式定义变量BreakCode,因此脚本引擎会在使用它的范围内自动定义变量。因此,您有两个变量BreakCode:一个在过程Breaks 中,另一个在过程Submit 中。后者永远不会被赋值,所以它保持为空。

      插入一行

      Dim BreakCode
      

      在这一行之后:

      Dim objFSO, wshShell, strUser, Name, LastName, strLogs, strLogFile, strLocation
      

      问题就会消失。

      【讨论】:

      • 除了 Ansgar,请,请,请在您的 VBScript 代码的第一行使用Option Explicit。每次你在没有option explicit 的情况下运行脚本时,一只小狗就会死去,这会让你避免很多此类错误。
      • 所以我还有一个问题.. 我补充说,但价值没有通过.. 对于 HowLate.Value 和 Reason.Value ?如果有什么不同,我还使用选项显式声明所有内容?很抱歉打扰你们..
      • 我现在知道为什么了...我必须先在字段中输入...然后点击提交...我该如何修改?
      • @PavleStojanovic 我不太确定您要什么。如果您想预先填写这些字段,您可以使用Window_OnLoad 程序来完成,否则您需要更详细地解释该行为与您期望/期望的行为有何不同。
      • 嘿 Ansgar,对不起...如果我首先选择“breakcode”,则设置值,但因为我什么都没写,所以它们是空的。我敢肯定它很简单,但我想不出它是怎么做的?
      最近更新 更多