In the interest of doing a https://stackoverflow.com/q/27159229/1168342 I am using a timer event to update the ribbon based on the paragraph style of where the cursor is.
However, if the user opens a dialog (e.g., Define New Number Format inside a numbered paragraph)
an exception occurs, {"Cannot create a Selection object when this dialog is active."}
, inside the timer event when trying to obtain the selection:
If Globals.ThisDocument.Application.Selection.Type <> WdSelectionType.wdSelectionInlineShape Then
Any access to Application.Selection
causes the exception (even checking it for Nothing
). So, I'm wondering how can I detect if a dialog is open?
Edit A workaround is to catch the System.Runtime.InteropServices.COMException
when trying to get Globals.ThisDocument.Application.Selection
. However, it seems better not to access this if a dialog is up and avoid the exception altogether.
It looks like you are trying to access the Word object model from secondary threads. But you shouldn't do so because Office applications uses the single threaded apartment model and doesn't support multithreading. You have to work with Word object on the main thread (UI thread). Use the timer class which fires the Tick event on the main thread (not secondary). See Comparing the Timer Classes in the .NET Framework Class Library for more information.