为了做https://stackoverflow.com/q/27159229/1168342,我正在使用计时器事件根据光标所在的段落样式更新功能区。
但是,如果用户打开一个对话框(例如,在编号段落中定义新数字格式)
在尝试获取选择时,在计时器事件中发生异常{"Cannot create a Selection object when this dialog is active."}
:
If Globals.ThisDocument.Application.Selection.Type <> WdSelectionType.wdSelectionInlineShape Then
对Application.Selection
的任何访问都会导致异常(甚至检查Nothing
)。所以,我想知道如何检测对话框是否打开?
修改解决方法是在尝试获取System.Runtime.InteropServices.COMException
时抓住Globals.ThisDocument.Application.Selection
。但是,如果对话框已启动并且完全避免异常,最好不要访问它。
答案 0 :(得分:0)
看起来您正在尝试从辅助线程访问Word对象模型。但是您不应该这样做,因为Office应用程序使用单线程单元模型并且不支持多线程。您必须在主线程(UI线程)上使用Word对象。使用timer类在主线程(不是辅助线程)上触发Tick事件。有关详细信息,请参阅Comparing the Timer Classes in the .NET Framework Class Library。