XL: Visual Basic Code to Resize an Application Window

ID: Q125266

5.00 5.00a 5.00c WINDOWS kbprg kbcode

The information in this article applies to:

SUMMARY

In Microsoft Windows, you cannot resize a window if it is already maximized or minimized (for example, if the Microsoft Excel window is maximized or minimized, you cannot choose the Size command from the Control menu to change the size of the window).

However, you can resize another application's window from a Visual Basic, Applications Edition, macro that contains at least two API (Application Programming Interface) calls. For an example of how to do this, see the sample macro code below.

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support professionals can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

Sample Visual Basic Code

'The following two Declare statements must each be entered on a single
'line.

Declare Sub movewindow Lib "USER" (ByVal hwnd%, ByVal x%, _
   ByVal y%, ByVal nwidth%, ByVal nheight%, _
   ByVal brepaint%)

Declare Function GetActiveWindow% Lib "USER" ()

Sub WindowResize()

   ' If there's an error in the line that tries to activate file manager
   ' go to the line that would help start it.
   On Error GoTo startfilemgr

   'Activate the file manager
   AppActivate "file manager"

   ' Get the window handle of the active application (File Manager
   ' in this example).
   hwnd = GetActiveWindow()

   'End the error checking routine
   Exit Sub

   ' Resize the file manager or the active application and place it in
   ' the upper-left corner of your screen

   movewindow hwnd, 3, 3, 500, 500, 1

   ' If there was an error starting File Manager use the following
   ' piece of the program.
startfilemgr:
   'Start File Manager
   shellid = Shell("c:\windows\winfile.exe", 1)

   'Activate File Manager
   AppActivate shellid

   'Get the Window Handle
   hwnd = GetActiveWindow

   'Resize the Window Handle.
   movewindow hwnd, 3, 3, 500, 500, 1

End Sub

For information about how to create a similar macro in the Microsoft Excel 4.0 macro language, please see the following article(s) in the Microsoft Knowledge Base:

   ARTICLE-ID: Q100360
   TITLE     : "APP.SIZE() Returns Error when Document Window is Maximized"

Additional reference words: 5.00 5.00c
Keywords          : kbprg xlvbahowto 
Version           : 5.00 5.00a 5.00c
Platform          : WINDOWS

Last Reviewed: May 17, 1999