Hi,

I am using API (VBA Excel v2106) to activate a workbook. In my example below I try to activate "Book3.xlsx".

Issue:
- Function lhWndWorkbookWindow() returns 0 if the workbook isn't already active.

NOTE!
In reality I am trying to activate correct workbook in Excel from coding in VBA Microsoft Access. I just wanted to simplify my case for this forum. It's the same issue having the code directly in VBA Excel.


Code:
'Show Application/Window on top of screen
Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long

'Find a child window with a given class name and caption
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) _
As Long

Sub ActivateMyWkb()
    
    Dim lhWndExcelWkb As Long
    
    'Activate "Book3.xlsx"
    lhWndExcelWkb = lhWndWorkbookWindow(Windows("Book3.xlsx"))
    BringWindowToTop (lhWndExcelWkb)
End Sub

Function lhWndWorkbookWindow(ByRef wndWindow As Window) As Long
'Get handle of Excel Workbook

    Dim lhWndExcel As Long
    Dim lhWndDesk As Long

    'Get the main Excel window
    lhWndExcel = Application.hwnd

    'Find the desktop
    lhWndDesk = FindWindowEx(lhWndExcel, 0, "XLDESK", vbNullString)

    'Find the workbook window
    lhWndWorkbookWindow = FindWindowEx(lhWndDesk, 0, "EXCEL7", wndWindow.Caption)
End Function