Solved

MoveWindow() in VBA in EXCEL

Posted on 2013-12-20
3
1,789 Views
Last Modified: 2013-12-21
I am using VBA in EXCEL under WinXPsp3 on a monitor with 1920x1200 resolution. The following VBA code module executes to open notepad and returns a handle but always fails to resize or move the notepad window. There are no error messages produced whether stepping through in the project browser or executing the code as a macro. The MoveWindow function may or may not return a nonzero value while stepping through the code. My goal is to open an instance of an application and set the size and location of the application window using the handle returned by the Shell function. What am I missing, or is there another way to accomplish this?
--------------------------------
Option Explicit
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Sub tile1()
Dim retval As Long, np_retval As Long
np_retval = Shell("C:\notepad.exe", vbNormalFocus)
retval = MoveWindow(np_retval, 960, 600, 600, 400, 1)
End Sub
--------------------------------
0
Comment
Question by:mdmat
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
Comment Utility
Shell() does not return a window handle that you need for MoveWindow.

I found some code (here: social.msdn.microsoft.com/forums/en-US/0665c896-fdf8-48d5-a9e2-f66f865772ad/how-can-i-get-a-handle-to-a-window-of-executed-process-in-vba) to find the window handle that belongs to the task you started.

Here's the full module code I used to test it, including your existing code.
Option Explicit

Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long

Public Const GW_HWNDNEXT As Long = 2
Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwprocessid As Long) As Long

Sub tile1()
    Dim retval As Long, np_retval As Long
    np_retval = Shell("C:\notepad.exe", vbNormalFocus)
    retval = MoveWindow(GetWinHandle(np_retval), 960, 600, 600, 400, 1) ' Application.hwnd
End Sub

Function ProcIDFromWnd(ByVal hwnd As Long) As Long
   Dim idProc As Long

   ' Get PID for this HWnd
   GetWindowThreadProcessId hwnd, idProc
   ProcIDFromWnd = idProc
End Function
     
Function GetWinHandle(hInstance As Long) As Long
   Dim tempHwnd As Long
     
   ' Grab the first window handle that Windows finds:
   tempHwnd = FindWindow(vbNullString, vbNullString)
  
   ' Loop until you find a match or there are no more window handles:
   Do Until tempHwnd = 0
      ' Check if no parent for this window
      If GetParent(tempHwnd) = 0 Then
         ' Check for PID match
         If hInstance = ProcIDFromWnd(tempHwnd) Then
            ' Return found handle
            GetWinHandle = tempHwnd
            ' Exit search loop
            Exit Do
         End If
      End If
  
      ' Get the next window handle
      tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT)
   Loop
End Function

Open in new window

0
 

Author Comment

by:mdmat
Comment Utility
An excellent response. Includes a terse and most relevant comment, a link, and sample code module.

Thank  you
0
 

Author Closing Comment

by:mdmat
Comment Utility
Directly on point and  prompt.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now