@@ -104,6 +104,7 @@ void _windowStateChangeHook_WinEventReceived(object sender, WinEventHook.WinEven
104104 switch ( className )
105105 {
106106 case _mainWindowClass :
107+ case _mainWindowClass2 :
107108 if ( e . EventType == WinEventHook . WinEvent . EVENT_OBJECT_CREATE ||
108109 e . EventType == WinEventHook . WinEvent . EVENT_OBJECT_DESTROY ||
109110 e . EventType == WinEventHook . WinEvent . EVENT_OBJECT_SHOW ||
@@ -205,6 +206,7 @@ void _windowStateChangeHook_WinEventReceived(object sender, WinEventHook.WinEven
205206 }
206207 }
207208
209+ // Runs on our automation thread
208210 void UpdateMainWindow ( IntPtr hWnd )
209211 {
210212 if ( MainWindow != hWnd )
@@ -218,37 +220,46 @@ void UpdateMainWindow(IntPtr hWnd)
218220 {
219221 // Either fresh window, or children not yet set up
220222
221- // Search for formulaBar
222- // I've seen ElementNotAvailbleException here during shutdown
223- AutomationElement mainWindow = AutomationElement . FromHandle ( hWnd ) ;
223+ //try // Anything can go wrong here, e.g. we're shutting down on the main thread
224+ //{
225+
226+ // Search for formulaBar
227+
228+ AutomationElement mainWindow = AutomationElement . FromHandle ( hWnd ) ;
224229#if DEBUG
225- var mainChildren = mainWindow . FindAll ( TreeScope . Children , Condition . TrueCondition ) ;
226- foreach ( AutomationElement child in mainChildren )
227- {
228- var hWndChild = ( IntPtr ) ( int ) child . GetCurrentPropertyValue ( AutomationElement . NativeWindowHandleProperty ) ;
229- var classChild = ( string ) child . GetCurrentPropertyValue ( AutomationElement . ClassNameProperty ) ;
230- // Debug.Print($"Child: {hWndChild}, Class: {classChild}");
231- }
230+ var mainChildren = mainWindow . FindAll ( TreeScope . Children , Condition . TrueCondition ) ;
231+ foreach ( AutomationElement child in mainChildren )
232+ {
233+ var hWndChild = ( IntPtr ) ( int ) child . GetCurrentPropertyValue ( AutomationElement . NativeWindowHandleProperty ) ;
234+ var classChild = ( string ) child . GetCurrentPropertyValue ( AutomationElement . ClassNameProperty ) ;
235+ // Debug.Print($"Child: {hWndChild}, Class: {classChild}");
236+ }
232237#endif
233- AutomationElement formulaBar = mainWindow . FindFirst ( TreeScope . Children ,
234- new PropertyCondition ( AutomationElement . ClassNameProperty , _formulaBarClass ) ) ;
235- if ( formulaBar != null )
236- {
237- FormulaBarWindow = ( IntPtr ) ( int ) formulaBar . GetCurrentPropertyValue ( AutomationElement . NativeWindowHandleProperty ) ;
238+ AutomationElement formulaBar = mainWindow . FindFirst ( TreeScope . Children ,
239+ new PropertyCondition ( AutomationElement . ClassNameProperty , _formulaBarClass ) ) ;
240+ if ( formulaBar != null )
241+ {
242+ FormulaBarWindow = ( IntPtr ) ( int ) formulaBar . GetCurrentPropertyValue ( AutomationElement . NativeWindowHandleProperty ) ;
238243
239- // CONSIDER:
240- // Watch WindowClose event for MainWindow?
244+ // CONSIDER:
245+ // Watch WindowClose event for MainWindow?
241246
242- FormulaBarWindowChanged ? . Invoke ( this , EventArgs . Empty ) ;
243- }
244- else
245- {
246- // Debug.Print("Could not get FormulaBar!");
247- }
247+ FormulaBarWindowChanged ? . Invoke ( this , EventArgs . Empty ) ;
248+ }
249+ else
250+ {
251+ // Debug.Print("Could not get FormulaBar!");
252+ }
253+ //}
254+ //catch (Exception ex)
255+ //{
256+
257+ //}
248258 }
249259 }
250260
251261 const string _mainWindowClass = "XLMAIN" ;
262+ const string _mainWindowClass2 = "EXCEL7" ; // What / when ????????
252263 const string _formulaBarClass = "EXCEL<" ;
253264 const string _inCellEditClass = "EXCEL6" ;
254265 const string _popupListClass = "__XLACOOUTER" ;
0 commit comments