Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Returning focus to Excel

34 views
Skip to first unread message

Tim Childs

unread,
Mar 15, 2002, 8:04:59 AM3/15/02
to
Hi

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


Tom Ogilvy

unread,
Mar 15, 2002, 8:35:22 AM3/15/02
to
Recently posted by Jim Rech in response to a similar question:

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

unread,
Mar 15, 2002, 8:38:34 AM3/15/02
to
did you try
ActiveWorkbook.Activate

Patrick Molloy
Microsoft Excel MVP

>.
>

Tim Childs

unread,
Mar 15, 2002, 9:20:56 AM3/15/02
to
Tom

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

Tim Childs

unread,
Mar 15, 2002, 11:20:33 AM3/15/02
to
Quick supplementary(!)

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


0 new messages