In the end, I ended up working with GetObject because it was not granular enough and wrote my own flickering knife, with some inspiration from osknows and great code examples from here and here . Thought I'd share it if others found it useful. Full module first
'looping through, parent and child (see also callbacks for lpEnumFunc) Private Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, _ ByVal lParam As Long) As Long Private Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As Long, _ ByVal lpEnumFunc As Long, _ ByVal lParam As Long) As Long 'title of window Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, _ ByVal lpString As String, _ ByVal cch As Long) As Long 'class of window object Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, _ ByVal lpClassName As String, _ ByVal nMaxCount As Long) As Long 'control window display Private Declare Function ShowWindow Lib "user32" (ByVal lHwnd As Long, _ ByVal lCmdShow As Long) As Boolean Private Declare Function BringWindowToTop Lib "user32" (ByVal hWnd As Long) As Long Private Declare Function SetFocus Lib "user32" (ByVal hWnd As Long) As Long Public Enum swcShowWindowCmd swcHide = 0 swcNormal = 1 swcMinimized = 2 'but activated swcMaximized = 3 swcNormalNoActivate = 4 swcShow = 5 swcMinimize = 6 'activates next swcMinimizeNoActivate = 7 swcShowNoActive = 8 swcRestore = 9 swcShowDefault = 10 swcForceMinimized = 11 End Enum 'get application object using accessibility Private Declare Function AccessibleObjectFromWindow Lib "oleacc" (ByVal hWnd As Long, _ ByVal dwId As Long, _ ByRef riid As GUID, _ ByRef ppvObject As Object) _ As Long Private Declare Function IIDFromString Lib "ole32" (ByVal lpsz As Long, _ ByRef lpiid As GUID) As Long 'Const defined in winuser.h Private Const OBJID_NATIVEOM As Long = &HFFFFFFF0 'IDispath pointer to native object model Private Const Guid_Excel As String = "{00020400-0000-0000-C000-000000000046}" Private Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(7) As Byte End Type 'class names to search by (Excel, in this example, is XLMAIN) Private mstrAppClass As String 'title (aka pathless filename) to search for Private mstrFindTitle As String 'resulting handle outputs - "default" app instance and child with object Private mlngFirstHwnd As Long Private mlngChildHwnd As Long '
And then some sample / test code.
Public Sub Test_GetExcelWbk() Dim MyXLApp As Object Dim MyXLWbk As Object Dim bleXLWasRunning As Boolean Dim bleWasOpen As Boolean Const TESTPATH As String = "C:\temp\MyFlickerbook.xlsx" Const SHOWONLOAD As Boolean = False Set MyXLWbk = GetExcelWbk(TESTPATH, SHOWONLOAD, bleWasOpen) If Not (MyXLWbk Is Nothing) Then Set MyXLApp = MyXLWbk.Parent bleXLWasRunning = MyXLApp.Visible If SHOWONLOAD = False Then If MsgBox("Show " & TESTPATH & "?", vbOKCancel) = vbOK Then MyXLApp.Visible = True MyXLApp.Windows(MyXLWbk.Name).Visible = True End If End If If bleWasOpen = False Then If MsgBox("Close " & TESTPATH & "?", vbOKCancel) = vbOK Then MyXLWbk.Close SaveChanges:=False If bleXLWasRunning = False Then MyXLApp.Quit End If End If End If End If Set MyXLWbk = Nothing Set MyXLApp = Nothing End Sub
Hope someone finds this helpful.
source share