Reputation: 51
I have a HTA that I want to run for four hours and pop into focus every hour as a reminder. The problem I'm having is that there's a Sub not recognising variables more than once. I'll post the code and then explain:
<HTML>
<HEAD>
<style type="text/css">
p {font-family: 'Segoe UI Light'; font-size: 12pt}
td {font-family: 'Segoe UI Light'; font-size: 12pt}
input {font-family: 'Segoe UI Light'; font-size: 12pt}
body {font-family: 'Segoe UI Light'; font-size: 12pt; color: #4D4C5C; background-color: white; background-image: url("MOEUpgrade.png")}
</style>
<TITLE>QT MOE Upgrade</TITLE>
<HTA:APPLICATION ID="MOEUpgrade"
APPLICATIONNAME="MOE Upgrade"
BORDER="dialog"
SCROLL="no"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="yes"
SYSMENU="no"
ICON="MOEUpgrade.ico">
</HEAD>
<SCRIPT LANGUAGE="VBScript">
Dim pbTimerID, pbHTML, pbWaitTime, pbHeight, pbWidth
Dim pbBorder, pbUnloadedColor, pbLoadedColor, pbStartTime
Dim iTimerID, strProcName, strProcID
Set objShell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE '%MOEUpgrade.hta%'")
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objProcess in colProcesses
Set objFile = objFSO.CreateTextFile("C:\MDT\ProcIDs.txt")
strProcName = objProcess.Name
strProcID = objProcess.ProcessID
objFile.WriteLine Now
objFile.WriteLine strProcName & " - " & strProcID
Next
Sub Window_OnLoad
pbWaitTime = 14400
pbHeight = 35
pbWidth= 760
pbUnloadedColor="white"
pbLoadedColor="#F69220"
pbBorder="#4D4C5C"
pbStartTime = Now
rProgressbar
pbTimerID = window.setInterval("rProgressbar", 200)
Set colItems = objWMIService.ExecQuery("Select * From Win32_VideoController WHERE AdapterDACType='Internal'")
For Each objItem in colItems
intHorizontal = objItem.CurrentHorizontalResolution
intVertical = objItem.CurrentVerticalResolution
Next
intLeft = (intHorizontal-1024)/2
intTop = (intVertical-600)/2
self.resizeto 1024,600
self.moveTo intLeft,intTop
self.focus()
iTimerID = window.setInterval("NagWindow",3000)
End Sub
Sub rProgressbar
pbHTML = ""
pbSecsPassed = DateDiff("s",pbStartTime,Now)
pbSecsToGo = Int(pbWaitTime - pbSecsPassed)
pbMinsToGo = Int(pbSecsToGo / 60)
pbHrsToGo = Int(pbMinsToGo / 60)
pbLoadedWidth = (pbSecsPassed / pbWaittime) * pbWidth
pbUnloadedWidth = pbWidth - pbLoadedWidth
pbHTML = pbHTML & "<table border=1 bordercolor=" & pbBorder & " cellpadding=0 cellspacing=0 width=" & pbWidth & "><tr>"
pbHTML = pbHTML & "<th width=" & pbUnloadedWidth & " height=" & pbHeight & "align=left bgcolor=" & pbLoadedColor & "></th>"
pbHTML = pbHTML & "<th width=" & pbLoadedWidth & " height=" & pbHeight & "align=left bgcolor=" & pbUnLoadedColor & "></th>"
pbHTML = pbHTML & "</tr></table>"
pbHTML = pbHTML & "<table border=0 cellpadding=0 cellspacing=0 width=" & pbWidth & "><tr>"
Select Case TRUE
Case pbSecsToGo <= 59
pbHTML = pbHTML & "<td align=center width=" & pbWidth & "% height=" & pbHeight & ">" & pbSecsToGo & " seconds remaining</td>"
Case pbSecsToGo > 60 And pbSecsToGo <= 119
pbSecsToGo = pbSecsToGo - (pbMinsToGo * 60)
pbHTML = pbHTML & "<td align=center width=" & pbWidth & "% height=" & pbHeight & ">" & pbMinsToGo & " minute, " & pbSecsToGo & " seconds remaining</td>"
Case pbSecsToGo >= 120 And pbSecsToGo <= 3599
pbSecsToGo = pbSecsToGo - (pbMinsToGo * 60)
pbHTML = pbHTML & "<td align=center width=" & pbWidth & "% height=" & pbHeight & ">" & pbMinsToGo & " minutes, " & pbSecsToGo & " seconds remaining</td>"
Case pbSecsToGo >= 3600 And pbSecsToGo <= 3659
pbSecsToGo = pbSecsToGo - (pbMinsToGo * 60)
pbMinsToGo = pbMinsToGo - (pbHrsToGo * 60)
pbHTML = pbHTML & "<td align=center width=" & pbWidth & "% height=" & pbHeight & ">" & pbHrsToGo & " hour, " & pbMinsToGo & " minute, " & pbSecsToGo & " seconds remaining</td>"
Case pbSecsToGo >= 3660 And pbSecsToGo <= 7199
pbSecsToGo = pbSecsToGo - (pbMinsToGo * 60)
pbMinsToGo = pbMinsToGo - (pbHrsToGo * 60)
pbHTML = pbHTML & "<td align=center width=" & pbWidth & "% height=" & pbHeight & ">" & pbHrsToGo & " hour, " & pbMinsToGo & " minutes, " & pbSecsToGo & " seconds remaining</td>"
Case pbSecsToGo >= 7200 And pbSecsToGo <= 7259
pbSecsToGo = pbSecsToGo - (pbMinsToGo * 60)
pbMinsToGo = pbMinsToGo - (pbHrsToGo * 60)
pbHTML = pbHTML & "<td align=center width=" & pbWidth & "% height=" & pbHeight & ">" & pbHrsToGo & " hours, " & pbMinsToGo & " minute, " & pbSecsToGo & " seconds remaining</td>"
Case pbSecsToGo >= 7260 And pbSecsToGo <= 10759
pbSecsToGo = pbSecsToGo - (pbMinsToGo * 60)
pbMinsToGo = pbMinsToGo - (pbHrsToGo * 60)
pbHTML = pbHTML & "<td align=center width=" & pbWidth & "% height=" & pbHeight & ">" & pbHrsToGo & " hours, " & pbMinsToGo & " minutes, " & pbSecsToGo & " seconds remaining</td>"
Case pbSecsToGo >= 10800 And pbSecsToGo <= 10859
pbSecsToGo = pbSecsToGo - (pbMinsToGo * 60)
pbMinsToGo = pbMinsToGo - (pbHrsToGo * 60)
pbHTML = pbHTML & "<td align=center width=" & pbWidth & "% height=" & pbHeight & ">" & pbHrsToGo & " hours, " & pbMinsToGo & " minute, " & pbSecsToGo & " seconds remaining</td>"
Case pbSecsToGo >= 10900
pbSecsToGo = pbSecsToGo - (pbMinsToGo * 60)
pbMinsToGo = pbMinsToGo - (pbHrsToGo * 60)
pbHTML = pbHTML & "<td align=center width=" & pbWidth & "% height=" & pbHeight & ">" & pbHrsToGo & " hours, " & pbMinsToGo & " minutes, " & pbSecsToGo & " seconds remaining</td>"
Case pbSecsToGo = 14430
NagWindow
End Select
pbHTML = pbHTML & "</tr></table>"
progressbar.InnerHTML = pbHTML
If DateDiff("s",pbStartTime,Now) >= pbWaitTime Then
StopTimer
StartUpgradeTimeout
End If
End Sub
Sub NagWindow
objFile.WriteLine Now
objFile.WriteLine strProcName & " - " & strProcID
objFile.Close
End Sub
Sub StopTimer
window.clearInterval(pbTimerID)
End Sub
Sub StartUpgradeTimeout
self.close()
End Sub
Sub StartUpgradeNow
If MsgBox ("Are you sure you want to start the upgrade now?",vbYesNo+vbExclamation,"Confirm Upgrade") = vbYes Then
self.close()
End If
End Sub
</SCRIPT>
<BODY>
<div align="justify">
<p>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</p>
</div>
<div align="center">
<span id = "progressbar"></span>
<br>
<span class="tooltip" title="Press OK to start the upgrade now"><input type="button" name="OKButton" value=" OK " onClick="StartUpgradeNow" style="font-family: 'Segoe UI Light'"></span>
</div>
</BODY>
</HTML>
The two variables in question are strProcName
and strProcID
, which are defined on line 28.
The NagWindow
procedure is called on line 69. At the moment, the interval is set to 3 seconds, but it will changed to one hour when I get it right.
Now in order to troubleshoot the issue, I'm creating a file to output the variable values. It starts in the For...Next
loop in lines 35-43 which creates the text file and outputs the variables strProcName
and strProcID
and the current time. This bit works, as does the first iteration of the NagWindow
Sub, where I again output the current time and variables to the same file, and then close it.
The contents of the file are as follows:
7/02/2018 2:14:04 PM mshta.exe - 13916 7/02/2018 2:14:07 PM mshta.exe - 13916
Interestingly, if I move the objFile.Close
operation from line 141 so it's now outside the Sub, it only loops once before failing, i.e. like it's only correctly processing the first objFile.WriteLine
operation on lines 40 & 41.
Eventually, I want to use the following command line inside the NagWindow
Sub:
objShell.AppActivate strProcName.strProcID
But that's once I've figured the issues with the variables.
Upvotes: 0
Views: 230
Reputation: 200293
You create (and open) the file at the beginning of your <script>
block (in the global scope), but close it in the procedure NagWindow
without ever re-opening it. Hence you try to write to an already closed file when you call NagWindow
for the second time.
Close the file after the For Each
loop where you create it and change
Sub NagWindow
objFile.WriteLine Now
objFile.WriteLine strProcName & " - " & strProcID
objFile.Close
End Sub
into
Sub NagWindow
Set objFile = objFSO.OpenTextFile("C:\MDT\ProcIDs.txt", 8)
objFile.WriteLine Now
objFile.WriteLine strProcName & " - " & strProcID
objFile.Close
End Sub
and the problem will disappear.
Upvotes: 1