I am trying to return foocus back to the main Excel window - how do it, pl
I think I must have overlooked the obvious but was any of the three
statements below, but none worked for me. They did not "error" either..
AppActivate ("Microsoft Excel")
AppActivate (Application.Name)
AppActivate (Application.Name) & " - " &
Application.ActiveWindow.Caption)
Regards
Tim
From: "Jim Rech" <jar...@kpmg.com>
References: <O3gHMP4vBHA.2112@tkmsftngp02>
Subject: Re: Switch Back to Excel
Date: Wed, 27 Feb 2002 07:22:21 -0500
Message-ID: <OgCCVl4vBHA.1592@tkmsftngp07>
Newsgroups: microsoft.public.excel.programming
Windows doesn't approve of inactive applications switching the focus back to
themselves. You should know that, Dave<g>.
Try this:
Public Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hWnd As Long, lpdwProcessId As Long) As Long
Public Declare Function AttachThreadInput Lib "user32" _
(ByVal idAttach As Long, ByVal idAttachTo As Long, _
ByVal fAttach As Long) As Long
Public Declare Function GetForegroundWindow Lib "user32" () _
As Long
Public Declare Function SetForegroundWindow Lib "user32" _
(ByVal hWnd As Long) As Long
Public Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) _
As Long
Public Declare Function ShowWindow Lib "user32" _
(ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Declare Function FindWindowA Lib "user32" _
(ByVal lpClassName As Long, ByVal lpWindowName As String) _
As Long
Const SW_SHOW = 5
Const SW_RESTORE = 9
''Run this an then switch to another application.
Sub Test()
Application.OnTime Now + TimeValue("00:00:05"), "BringTotop"
End Sub
Sub BringToTop()
ForceForegroundWindow FindWindowA(0, Application.Caption)
End Sub
''All this is necessary in Windows 98/NT5 because MS disabled
''SetForegroundWindow unless
''the caller is itself the foreground window.
''From Visual Basic Programmers Journal Feb 1999 pg 94
Function ForceForegroundWindow(ByVal hWnd As Long) As Boolean
Dim ThreadID1 As Long
Dim ThreadID2 As Long
Dim nRet As Long
If hWnd = GetForegroundWindow Then
ForceForegroundWindow = True
Else
ThreadID1 = GetWindowThreadProcessId( _
GetForegroundWindow, ByVal 0&)
ThreadID2 = GetWindowThreadProcessId(hWnd, ByVal 0&)
If ThreadID1 <> ThreadID2 Then
AttachThreadInput ThreadID1, ThreadID2, True
nRet = SetForegroundWindow(hWnd)
AttachThreadInput ThreadID1, ThreadID2, False
Else
nRet = SetForegroundWindow(hWnd)
End If
If IsIconic(hWnd) Then
ShowWindow hWnd, SW_RESTORE
Else
ShowWindow hWnd, SW_SHOW
End If
ForceForegroundWindow = CBool(nRet)
End If
End Function
--
Jim Rech
Excel MVP
--
Regards,
Tom Ogilvy
"Tim Childs" <tsn...@yahoo.co.uk> wrote in message
news:#yzWyHCzBHA.1652@tkmsftngp04...
Patrick Molloy
Microsoft Excel MVP
>.
>
It did the trick when I incorporated it. I seemed to need to retain the
ontime part of the statement* to make it work - I couldn't simply call
"BringtoTop"
* I imagined this was so I could do the test and select another application
MANY thanks
Tim
============
Patrick
I hadn't tried but I have now and, unfortunately, it did not do the trick.
Thanks anyway
Tim
Don't people writing VB code want to go into another application, do
something in that application, then come back to Excel (or whichever Office
program they started in)? seems perfectly natural to me.
Tim