excelvba

VBA API declarations. Bring window to front , regardless of application


I am trying to bring and Excel window to the front of all applications running regardless.

Current code,

Private Declare Function SetForegroundWindow _
                     Lib "user32" _
                   (ByVal hWnd As Long) As Long

Public Sub Bring_to_front()   
    SetForegroundWindow wb.Application.hWnd    
End Sub
Sub Test()    
     Set wb = Workbooks("MyWorkBook.xlxs")
      call Bring_to_front
End Sub

At the moment nothing happens.


Solution

  • Found the answer to what I as trying to do after a bit more research.

    This will bring the worksheet you specify to the front.

    Public Declare Function SetForegroundWindow _
    Lib "user32" (ByVal hwnd As Long) As Long
    
    Public Sub Bring_to_front()
        Dim setFocus As Long
    
        ThisWorkbook.Worksheets("Sheet1").Activate
        setfocus = SetForegroundWindow(Application.hwnd)
    End Sub