Solved

MoveWindow() in VBA in EXCEL

Posted on 2013-12-20
3
2,131 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
ID: 39733455
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
ID: 39734013
An excellent response. Includes a terse and most relevant comment, a link, and sample code module.

Thank  you
0
 

Author Closing Comment

by:mdmat
ID: 39734016
Directly on point and  prompt.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

679 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