+ Reply to Thread
Results 1 to 9 of 9

Bring Excel to foreground from VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    06-08-2010
    Location
    INDIA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Bring Excel to foreground from VBA

    Hi,
    I am trying to force Excel window as foregrond from VBA window using some win32 API.But this is not helping out.I followed the steps below.

    1.Open Excel
    2.Press ALT+F11 to open VBA window
    3.Double click the ThisWorkbook and paste the following code in the coding area

    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    Public Sub Test()
    Dim exlHandle As Long
    Dim exlTitle As String
    exlTitle = Application.Caption
    exlHandle = FindWindow(vbNullString, exlTitle)
    SetForegroundWindow (exlHandle)
    End Sub

    Call the method Test from VBA immediate window like below and press enter key there.

    call ThisWorkbook.Test

    It suppose to set Excel as forground but not.Any suggestion?

    Regards,
    Rajanbabu

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,439

    Re: Bring Excel to foreground from VBA

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    From a post by Jim Rech.

    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 Test2()
    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
    
    Debug.Print hWnd
    
        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
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    06-08-2010
    Location
    INDIA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Bring Excel to foreground from VBA

    Thanks for the reply.But this is also not helping out.
    My need is either one of the below
    1.Set excel as foreground window or
    2.Set VBA as background window

    -Rajanbabu

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,439

    Re: Bring Excel to foreground from VBA

    No need to quote my post. It just waste space.

    The code works for me.
    What happens for you. Code runs but does not bring window to foreground.
    Or does it error?
    What if you put a breakpoint in the code does it even run?

  5. #5
    Registered User
    Join Date
    06-08-2010
    Location
    INDIA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Bring Excel to foreground from VBA

    I kept breakpoint in method Sub Test2() and pressed F5 button.But it is throwing error like below
    "The macro 'Book1!BringTotop' can not be found.

    but i kept breakpoint in the method BringTotop() and run over.But didnt set Excel as foreground

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,439

    Re: Bring Excel to foreground from VBA

    this workbook works for me.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    06-08-2010
    Location
    INDIA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Bring Excel to foreground from VBA

    I tried the same steps in my friend machine which is having Excel 2003,SP2.But still seeing the same behavior.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1