How Can I Specify a Window Size for My HTA?

How Can I Specify a Window Size for My HTA?

  • Comments 2
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I specify a particular window size for my HTA?

-- RN

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RN. We can’t speak for Dear Abby, Ann Landers or any other daily columnist, but the Scripting Guys are always on the lookout for a question that can be answered using a single line of code. That’s the Holy Grail for daily columnists: one and done. And, to the best of our knowledge, neither Dear Abby nor Ann Landers have ever answered a question using just a single line of code. It’s always been our dream to beat them to the punch.

And now we have. How can you specify a particular window size for an HTA? Here’s how:

window.resizeTo 400,250

There you go: add that line of code to your HTA and the next time you start the thing the window will automatically be sized to 400 pixels wide by 250 pixels high.

What’s that? Well, OK, sure, if you want to be picky about it. Admittedly, you can’t just add that line of code anywhere within your HTA; instead, you’ll need to create a Window_onLoad subroutine and place the code inside there:

Sub Window_onLoad
    window.resizeTo 400,250
End Sub

As you probably know, Window_onLoad is a special subroutine designed to run any time the HTA starts up or is refreshed. If you place the resizeTo method inside Window_onLoad then that line of code will execute each time the HTA starts. In turn, that means that the window will be appropriately sized each time the HTA starts.

Here’s a more “complete” HTA, one that does nothing other than show up in a window 400 pixels by 250 pixels:

<SCRIPT LANGUAGE="VBScript">

    Sub Window_onLoad
        window.resizeTo 400,250
    End Sub 

</SCRIPT>

<BODY>
</BODY>

Simply copy the code, paste it into Notepad, and then save the file with a .HTA file extension. If you aren’t sure what happens then, well, you might want to take a look at Part 1 in our series on creating HTAs. (Which, in case you’re wondering, is short for HTML Applications.)

So, yes, technically, we really needed to add three lines of code in order to get this to work:

Sub Window_onLoad
    window.resizeTo 400,250
End Sub

But do us a favor, OK? The next time Ann Landers asks, tell her we answered this question with just a single line of code. We can’t wait to see the look on her face when she hears that.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Great information it was very useful.

  • Thank you for this explanation and the great series on creating HTAs!