Results 1 to 9 of 9

Open an application and set its window position with VBA

This is a discussion on Open an application and set its window position with VBA within the Excel Questions forums, part of the Question Forums category; Hello, I'm trying to create a macro in VBA using excel 2003 and/or 2010 to launch an external application. My ...

  1. #1
    New Member
    Join Date
    Aug 2013
    Posts
    9

    Question Open an application and set its window position with VBA

    Hello,

    I'm trying to create a macro in VBA using excel 2003 and/or 2010 to launch an external application. My system has 4 monitors attached to it and I would like to be able to position the application on a specific part of one of the monitors once it is opened.

    Currently I am using

    Dim Filename As String
    Dim retVal As Variant
    Filename = "filename.entension"
    retVal = Shell("C:\Program Files\Program.exe " & Filename, vbMaximizedFocus)

    This opens the correct file with the program but it only ever opens on monitor 1 (and not maximized either I might add)

    I've managed to get Internet Explorer to open where I want by using
    Set objIE = CreateObject("InternetExplorer.Application")
    With objIE
    .navigate "www.yahoo.com"
    .visible = true
    .Top = -1080
    .Left = 3840
    .Height = 1080
    .Width = 1920
    End With

    Is it possible to do something similar with "Program.exe"? The program in question is a third party business application, not a microsoft application or anything like that..

  2. #2
    Board Regular
    Join Date
    Nov 2008
    Location
    Romania/Netherlands
    Posts
    2,488

    Default Re: Open an application and set its window position with VBA

    The Shell command should return a handle to the window.

    I would try two things: first try:
    Set RetVal = Shell(etc)

    Step through it to see if it creates an error here. If not then Retval should be the object and you can use it now just like ObjIE in your other example.

    If it does create an issue, then look at the nearly last post in this thread, where you can see how to get a handle. You can then use Win API calls to move the application (A handle is as it sounds, the thing with which you can manipulate the application)

    shell commands in excel

  3. #3
    New Member
    Join Date
    Aug 2013
    Posts
    9

    Default Re: Open an application and set its window position with VBA

    The program steps through everything I have so far and executes the shell command. However from that point trying to set Retval.top = 100 returns an object required error. I tried following the other thread you linked but I am at a loss on that. May need to do some googling on Win API calls if that is going to be the best course of action. Thanks.

  4. #4
    Board Regular
    Join Date
    Nov 2008
    Location
    Romania/Netherlands
    Posts
    2,488

    Default Re: Open an application and set its window position with VBA

    Controlling external programs using vba
    that may be the ticket. Not within Excel, but still for you to launch everything as you want it

    I have tried it with limited success thorough VBA and the windows API. Most programs use their last position as their next start-up position, and will ignore position & size info at start-up. Which means you will need to get the handle properly to do it. This is how far I got. See the comments in the code for additional information.

    Option Explicit

      Private Type PROCESS_INFORMATION
         hProcess As Long
         hThread As Long
         dwProcessId As Long
         dwThreadId As Long
      End Type
        ' STARTUPINFO info: _
          http://msdn.microsoft.com/en-us/library/windows/desktop/ms686331(v=vs.85).aspx

      Private Type STARTUPINFO
         cb As Long
         lpReserved As String
         lpDesktop As String
         lpTitle As String
         dwX As Long
         dwY As Long
         dwXSize As Long
         dwYSize As Long
         dwXCountChars As Long
         dwYCountChars As Long
         dwFillAttribute As Long
         dwFlags As Long
         wShowWindow As Integer
         cbReserved2 As Integer
         lpReserved2 As Long
         hStdInput As Long
         hStdOutput As Long
         hStdError As Long
      End Type

        ' CreateProcess info: _
          http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx

      Private Declare Function CreateProcess Lib "kernel32" _
         Alias "CreateProcessA" _
         (ByVal lpApplicationName As String, _
         ByVal lpCommandLine As String, _
         lpProcessAttributes As Any, _
         lpThreadAttributes As Any, _
         ByVal bInheritHandles As Long, _
         ByVal dwCreationFlags As Long, _
         lpEnvironment As Any, _
         ByVal lpCurrentDriectory As String, _
         lpStartupInfo As STARTUPINFO, _
         lpProcessInformation As PROCESS_INFORMATION) As Long

      Private Declare Function OpenProcess Lib "kernel32.dll" _
         (ByVal dwAccess As Long, _
         ByVal fInherit As Integer, _
         ByVal hObject As Long) As Long

      Private Declare Function TerminateProcess Lib "kernel32" _
         (ByVal hProcess As Long, _
         ByVal uExitCode As Long) As Long

      Private Declare Function CloseHandle Lib "kernel32" _
         (ByVal hObject As Long) As Long

      Const SYNCHRONIZE = 1048576
      Const NORMAL_PRIORITY_CLASS = &H20&
      Const STARTF_USEPOSITION = &H4&
      Const STARTF_USESIZE = &H2&
      

      Private Sub Form_Click()
         Dim pInfo As PROCESS_INFORMATION
         Dim sInfo As STARTUPINFO
         Dim sNull As String
         Dim lSuccess As Long
         Dim lRetValue As Long

         With sInfo
            .cb = Len(sInfo)
            'set flag to tell that size and position info _
             has been set in structure

            .dwFlags = STARTF_USESIZE + STARTF_USEPOSITION
            ' set position and size
            .dwX = 200
            .dwY = 100
            .dwXSize = 800
            .dwYSize = 400
        End With
         lSuccess = CreateProcess(sNull, _
                                 "C:\Program Files\7-Zip\7zFM.exe", _
                                 ByVal 0&, _
                                 ByVal 0&, _
                                 1&, _
                                 NORMAL_PRIORITY_CLASS, _
                                 ByVal 0&, _
                                 sNull, _
                                 sInfo, _
                                 pInfo)
         If lSuccess = False Then MsgBox "launch failed"
         lRetValue = CloseHandle(pInfo.hThread)
         lRetValue = CloseHandle(pInfo.hProcess)

      End Sub
          
          


    a link explaining about the failure to use position info:
    Answer : CreateProcess ignores dwX,dwY parameters in STARTUPINFO

  5. #5
    Board Regular
    Join Date
    Nov 2008
    Location
    Romania/Netherlands
    Posts
    2,488

    Default Re: Open an application and set its window position with VBA

    This thread may show another method.

    link: Control IE with handle

  6. #6
    Board Regular
    Join Date
    Nov 2008
    Location
    Romania/Netherlands
    Posts
    2,488

    Default Re: Open an application and set its window position with VBA


  7. #7
    New Member
    Join Date
    Aug 2013
    Posts
    9

    Default Re: Open an application and set its window position with VBA

    Thanks for your help sijpie, I tried to follow the code you posted in post #4, but as you linked to in another thread, it will not control the window size and position it just defaults to the last known position.

    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
      
     Private Declare Function MoveWindow Lib "user32.dll" (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
    
    Dim lSuccess As Long
    Dim lHwnd As Long
     
        lHwnd = FindWindow("wndClass_desked_gsk", vbNullString)
        Debug.Print lHwnd
    
    
     lSuccess = MoveWindow(ByVal 6752804, ByVal 0, ByVal 0, ByVal 1920, ByVal 1200, ByVal 1)
    If lSuccess = False Then MsgBox "failed"
    This code I used to find the hwnd of the open excel window and from there I was able to move it and reposition it, which works fine, although I'm not sure how to modify the code to find hwnd of other running programs (some kind of search by window title would probably be ideal). Also it seems like the hwnd of the open window changed when I closed it and re-opened it. I'm not very familiar with what is going on here, this code is pretty advanced for me but I guess it wouldnt be too hard to just have it look for the current hwnd and use that if it changes every time..?

  8. #8
    Board Regular
    Join Date
    Nov 2008
    Location
    Romania/Netherlands
    Posts
    2,488

    Default Re: Open an application and set its window position with VBA

    << Also it seems like the hwnd of the open window changed when I closed it and re-opened it. >>
    Yes, the handle is not attached to the application name or so, you can see it as a pointer into the memory where the details of this particular instance of a window can be found. So if you close a window, then the handle becomes invalid. And on opening the program again, it is unlikely that Windows would put the new handle in exactly the same place in memory
    Short Guide to Better VBA - Link: http://www.mrexcel.com/forum/showthread.php?t=712119

    Please use code tags around your code:
    [Code] Your code here... [/Code]

  9. #9
    Board Regular
    Join Date
    Nov 2008
    Location
    Romania/Netherlands
    Posts
    2,488

    Default Re: Open an application and set its window position with VBA

    To find the handle of a window using the windows Caption (title) use
    Code:
    lHwnd = FindWindow(0&, “Caption of Window searched for”)


    So it seems if you want to use the windows tietl youhave to set the first parameter to a (long) 0

    See also these links:
    API FindWindow doesn't Find Window Defined by Caption • ExcelKey.com
    Daily Dose of Excel » Blog Archive » FindWindow
    Short Guide to Better VBA - Link: http://www.mrexcel.com/forum/showthread.php?t=712119

    Please use code tags around your code:
    [Code] Your code here... [/Code]

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com