Solved

MoveWindow() in VBA in EXCEL

Posted on 2013-12-20
3
2,194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This collection of functions covers all the normal rounding methods of just about any numeric value.
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 viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

734 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