Search code examples
vbscriptrestorehtaminimize

Restore a minimised application via VBS within an HTA


I have an HTA that is minimised while it carries out a backup. After this backup is complete I'd like to restore the HTA, but I'm having trouble.

I've tried a few things (below), but with no success. Is anyone able to point me towards a definitive solution?

First I tried to simply activate the HTA, but this failed. I'm no expert, but my understanding is that doing this should mimick a mouse click on the HTA in the task bar, thus restoring it -

Sub RestoreBackupHTA()

    Shell.AppActivate "Backup"

End Sub

Next I tried to activate the HTA and then send the keys to "restore" the active window, but that also failed -

Sub RestoreBackupHTA()

    Shell.AppActivate "Backup"
    Shell.SendKeys "% r"

End Sub

Finally I tried to first activate the HTA and then send the keys to "restore" the active window after a short timeout (I found a couple of posts suggesting this could help to ensure that the HTA was fully active before sending the keys to restore it), but again, failure -

Sub RestoreBackupHTA()

    Shell.AppActivate "Backup"
    Call window.setTimeout("RestoreBackupHTAAfterWait", 500, "VBScript")

End Sub

Sub RestoreBackupHTAAfterWait()

    '** Create a tempory Shell object (required as the global Shell object is out of scope for some reason *'
    Dim tmpShell
    Set tmpShell = CreateObject("Wscript.Shell")

    tmpShell.SendKeys "% r" ' Restore the HTA

    Set tmpShell = Nothing  ' Destroy the tmpShell object

End Sub

Notes -

  1. The HTA has the ID "Backup" (<HTA:APPLICATION ID="Backup" ... />, and so "Backup" is displayed in the title bar when it is running (and as the title in the task manager). This is why I'm using it as thetitleparameter forShell.AppActivate`, and according to this MSDN page that should be correct.
  2. Shell is declared and set globally, thus it is available to the RestoreBackupHTA procedure in the examples above.

Solution

  • Hmm. It seemed to work for me. My window title shows the full path to the hta, so I just tried the following (after using setTimeout):

    With CreateObject("WScript.Shell")
        .AppActivate("test.hta")
        .SendKeys "% r"
    End With
    

    And didn't have any problems.

    But here's another one way. You can use nircmd (always handy to have on hand).

    CreateObject("WScript.Shell").Run "nircmd.exe win normal ititle Backup"
    

    This will restore any windows with "Backup" in the title. You can filter many ways (title, class, PID, etc) if you need finer control.