2013-10-17 102 views
1

我有一个宏,它在事件SelectionChange上调用。这个宏必须检查,附加到文档的模板。有可能,打开文档的计算机上不存在附加的模板。我需要知道,当发生这种情况时,所以我不能使用ActiveDocument.AttachedTemplate(当模板不存在时它将显示简单的Normal.dot)。所以,我用:当其他对话框打开时,我可以调用对话框吗?

Application.Dialogs(wdDialogToolsTemplates).Template 

而且工作正常。 但是,当我尝试通过Ctrl + F在文档中查找某些内容时,搜索和事件触发时选择会发生变化。宏被调用,但上线上面我得到一个错误:

This method or property is not available because the find and replace dialog box is active

所以,问题是 - 是否有使用这个属性的一种方式,而查找和替换对话框处于活动状态...?或者mabe - 如果查找和替换对话框处于活动状态,有没有办法检查?

+0

您认为如何在代码中添加'On Error Resume Next'? –

+0

嗯,我可以做到这一点;)如果我找不到合适的解决方案,我会。 – aurel

回答

1

正如我在评论中所建议的,您可以尝试使用On Error Resume Next来消除您的错误。但是,我做了一些测试,这对我所发现的有趣。您可以通过两种方式添加错误处理,这会产生不同的结果。

'1st attempt will keep Find-Replace window and it will omit error 
On Error Resume Next 
Debug.Print Application.Dialogs(wdDialogToolsTemplates).Template 
On Error Goto 0 


'2nd attempt will close Find-Replace window and will return template name 
On Error Resume Next  'this seems to be unnecessary anyway 
Dim tmpDialog As Dialog 
Set tmpDialog = Application.Dialogs(wdDialogEditFind) 
'Find-Replace window will be closed at this stage 
Debug.Print Application.Dialogs(wdDialogToolsTemplates).Template 

试过并测试Office-Word-2010。