0

I have an HTA that I want to pop into focus every hour as a reminder in case it's minimised or has the focus taken away from it. The problem I'm having is there's a Sub that's not recognising variables. 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}
</style>

<TITLE>QT MOE Upgrade</TITLE>

<HTA:APPLICATION ID="MOEUpgrade" 
    APPLICATIONNAME="MOE Upgrade" 
    BORDER="dialog"
    SCROLL="no"
    SHOWINTASKBAR="yes"
    SINGLEINSTANCE="yes"
    SYSMENU="no">
</HEAD>

<SCRIPT LANGUAGE="VBScript">

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%'")

For Each objProcess in colProcesses

    strProcName = objProcess.Name
    strProcID = objProcess.ProcessID

Next

Sub Window_OnLoad

    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",5000)

End Sub

Sub NagWindow

    MsgBox strProcName & VBCRLF & strProcID

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">
    <br>
    <span class="tooltip" title="Click 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>

If you're so inclined, you can save this code as MOEUpgrade.hta and it should run for you without any issues. This code works and pops up a message box showing the strProcName and strProcID variables as expected, but when I change line 58 inside the NagWindow Sub to:

objShell.AppActivate strProcName.strProcID

It fails with an object required ('strProcName') error. Does anyone know why the variables are not being recognised when using the objShell function please but are when using MsgBox?

JAG
  • 41
  • 1
  • 5
  • What’s the dot for? `strProcID` isn’t a property of `strProcName`. Which is why you get the error `strProcName` is not an object reference, it’s a string variable. `AppActivate` takes a string that represents the Window Title you want to activate, so maybe you meant `objShell.AppActivate strProcName & “.” & strProcID` instead? – user692942 Feb 08 '18 at 23:03
  • It won't pop in to focus. Windows won't let you steal focus from the user's program. `AppActivate` is a wrapper for `SetForegroundWindow()`. Read the rules here https://msdn.microsoft.com/en-us/library/windows/desktop/ms633539%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396 – ACatInLove Feb 08 '18 at 23:09
  • You can add a process ID to the AppActivate command. It didn't seem to work when calling the window name by itself, so I went with the other method. See here: https://msdn.microsoft.com/fr-fr/library/x78640t0(v=vs.84).aspx – JAG Feb 08 '18 at 23:28
  • So if AppActivate isn't the right method to use, can someone please suggest what would be? I tried self.focus () and that worked, but ONLY if the window had been minimised. If another window was on top of it, it didn't work. – JAG Feb 08 '18 at 23:31
  • Lankymart, I tried your suggestion and while I don't get the error anymore, the window doesn't appear. So thanks, you have solved the error I was getting but as @ACatInLove said, it doesn't seem like AppActivate is the correct method to use. – JAG Feb 08 '18 at 23:49
  • Remember you can change the window, just not make it the window with focus. But HTAs don't really have access to those types of commands. – ACatInLove Feb 09 '18 at 00:30
  • Can you please clarify the meaning of "you can change the window"? Does that mean the HTA will become visible to the user if it's been minimised or has other window(s) on top of it? – JAG Feb 09 '18 at 03:54
  • You can set the always on top to make it inactive but on top on non ontop windows. The issue is focus not visibility. Can you restart your program using `CreateObject("Shell.Application").ShellExecute "C:\myfile.hta"` – ACatInLove Feb 09 '18 at 04:13
  • See https://stackoverflow.com/questions/27248528/size-batch-windows-and-set-in-specific-location on the general way to manipulate windows. – ACatInLove Feb 09 '18 at 04:16
  • Possible duplicate of [WshShell.AppActivate doesn't seem to work in simple vbs script](https://stackoverflow.com/questions/23165377/wshshell-appactivate-doesnt-seem-to-work-in-simple-vbs-script) – user692942 Feb 09 '18 at 07:22

1 Answers1

0

The reason for the error

Object required: 'strProcName'

is because you try to call strProcID as an object property of strProcName but it is clear from the code that both strProcName and strProcID are string variables.

If you are trying to use AppActivate with the Window Process Id you likely want to try;

Call objShell.AppActivate(strProcID)

Useful Links

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175