Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bringing external application window to top (maximizing) after it has been minimized

Status
Not open for further replies.

FrankMars

Technical User
Dec 20, 2010
67
US
I have 4 buttons on a form, each of which open a different file in a single external application. When I click the button to open File 1, the external application and file open on top as desired. After doing some work on that file, I minimize the external application and return to my Access form. When I later wish to open File 2 in the external application, I click on that button and the file opens but the external application remains minimized. Would it be possible to have the external application and file open on top every time a "open file" button is clicked? This is the code I have to open each file thus far:


Private Sub Open File 1_Click()

Dim X As Variant
Dim Path As String
Dim File As String

Dim a As Long

a = 1

Path = "C:\Program Files\Chief Architect\Chief Architect Premier X5 (64 bit)\Chief Architect Premier X5.exe"

If a = 1 Then
File = [File1]
X = Shell(Path + " " + File, vbMaximizedFocus)
Else
MsgBox "Error"
End If

End Sub


Thank you in advance.
 
How are ya FrankMars . . .

This can be done with two API functions. [blue]FindWindow[/blue] & [blue]BringWindowToTop[/blue].

For starters copy/paste the following to the declaration section of the forms code module:

Code:
[blue]Private Declare Function BringWindowToTop Lib "user32" _
                         (ByVal hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
                         (ByVal lpClassName As Any, _
                          ByVal lpWindowName As Any) As Long[/blue]

Next copy/paste the following function to the same code module:

Code:
[blue]Public Function WinToTop([purple][b]WindowTitle[/b][/purple] As String)
   Dim iret As Long, THandle As Long
   
   THandle = FindWindow(vbEmpty, [purple][b]WindowTitle[/b][/purple])
   If THandle <> 0 Then BringWindowToTop (THandle)
End Function[/blue]

To bring any window to the top, just execute the function (be sure to pass the [purple]windowtitle[/purple]). Examples would look like:

Code:
[blue]   [green]'Unsaved Notepad[/green]
   WinToTop "Untitled - NotePad"

   [green]'Calculator[/green]
   WinToTop "Calculator"[/blue]

Be careful as I suspect the window title will change with the active file. ... Thats It ...

[blue]Your Thoughts? . . .[/blue]


See Ya . . .

Be sure to see FAQ219-2884 Worthy Reading! [thumbsup2]
Also FAQ181-2886 Worthy Reading! [thumbsup2]
 
Thanks AceMan. I'm trying to implement your code but am having problems. Access isn't accepting the WindowTitle. The title bar of the window literally reads; Chief Architect Premier X5 - [Hamilton Comps1: Floor Plan]. I've tried underlines, single and double quotes, etc. Can you tell me what the WindowTitle should be?

 
FrankMars . . .

Its [blue]double quotes[/purple] for the [blue]"[/blue]window title[blue]"[/blue]. Thats a pretty hefty Title ya got there. To be sure it only takes one missing or wrong character for it not to work. Why not try something easier like the [blue]Calculator[/blue] ... just to be sure its working.

[blue]Your Thoughts? . . .[/blue]

See Ya . . .

Be sure to see FAQ219-2884 Worthy Reading! [thumbsup2]
Also FAQ181-2886 Worthy Reading! [thumbsup2]
 
Thanks AceMan, that's just what I did with Calculator. The Calculator does come up to top when it is behind Access. However, when Calculator has first been minimized, it only flashes on the taskbar and does not come to top. Is there a way to have it come to top after it has been minimized to the taskbar?
 
FrankMars . . .

So sorry ... forgot to test while minimized. Thats gonna add a few more API's and a modification in code. Replace the prior declarations with the following:
Code:
[blue]Private Declare Function BringWindowToTop Lib "user32" _
                         (ByVal hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
                         (ByVal lpClassName As Any, _
                          ByVal lpWindowName As Any) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1[/blue]
And WinToTop be comes:
Code:
[blue]Public Function WinToTop(WindowTitle As String)
   Dim iret As Long, THandle As Long
   
   THandle = FindWindow(vbEmpty, WindowTitle)
   If THandle <> 0 Then
      If IsIconic(THandle) > 0 Then
         ShowWindow THandle, SW_SHOWNORMAL
      Else
         BringWindowToTop (THandle)
      End If
   End If

End Function[/blue]

Are you still having the problem with [blue]Chief Architect Premier X5 - [Hamilton Comps1: Floor Plan]?[/blue]

[blue]Your Thoughts? . . .[/blue]

See Ya . . .

Be sure to see FAQ219-2884 Worthy Reading! [thumbsup2]
Also FAQ181-2886 Worthy Reading! [thumbsup2]
 
Thanks AceMan, works great! Also, the window title is now working ok.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top