Keng
Keng

Reputation: 53101

How can I maximize, restore, or minimize a window with a vb script?

I need to be able to make separte .vbs files that will (when triggered with a keyboard short-cut) will make the active window maximized, minimized, or restored.

How can I do this without downloading and installing (not allowed here) a separate package.

Upvotes: 14

Views: 98564

Answers (5)

user1328876
user1328876

Reputation:

This works for me in my Excel macro to maximise an external PDF document by executing the shortcut keys 'Alt+Spacebar+x'.

Mote: '%' represents the Alt key, '( )' represents the spacebar key and 'x' represents the maximise key.

Application.SendKeys "%+( )+(x)", True

Upvotes: 1

dmitrycello
dmitrycello

Reputation: 184

Topic is old. But I managed to find language independent solution. SendKeys can basically send any keys to application, including arrow keys and enter key. So we can emulate those actions without particular letters (x,r,n). Here is working example:

Dim oShell : Set oShell = CreateObject("WScript.Shell")
oShell.SendKeys("% {DOWN}{DOWN}{DOWN}{DOWN}{ENTER}")   'Maximize
'...
oShell.SendKeys("% {ENTER}")   'Restore
'...
oShell.SendKeys("% {DOWN}{DOWN}{DOWN}{ENTER}")   'Minimize

Upvotes: 1

Bisweswar Swain
Bisweswar Swain

Reputation: 11

To maximize any window, the following code will work:

Application.SendKeys "%{ }"
Application.Wait (Now + TimeValue("00:00:02"))
Application.SendKeys "x"

Upvotes: 1

Abraham
Abraham

Reputation: 41

SendKeys was not working in my computer. Spanish native with Spanish and English keyboard. I did this and worked in my code as instruction and worked to maximize my excel window. I put the .Sleep to visually check it.

objExcel.SendKeys"% x"
objExcel.Visible = True
objExcel.SendKeys"% x"

WScript.Sleep 2000

Upvotes: 4

Helen
Helen

Reputation: 97697

VBScript and Windows Script Host don't provide intrinsic functions for maximizing/minimizing/restoring a window. Without any third-party tools, your only option is to use SendKeys to simulate keyboard the shortcuts of the corresponding commands in a window's system menu.

  • To maximixe the active window, you can simulate the Alt+SpaceBar, x shortcut:

    Set oShell = CreateObject("WScript.Shell")
    oShell.SendKeys "% x"
    
  • To minimize the active window, use Alt+SpaceBar, n:

    Set oShell = CreateObject("WScript.Shell")
    oShell.SendKeys "% n"
    
  • To restore the active window, use Alt+SpaceBar, r:

    Set oShell = CreateObject("WScript.Shell")
    oShell.SendKeys "% r"
    

(Note that this code won't work in non-English Windows versions, where the names of the Maximize/Minimize/Restore commands are localized and therefore have other shortcuts.)

Upvotes: 25

Related Questions