方法'Display Alerts'对象'_Application Failed'错误

我在一个excel文件中写了一个函数“TestFunction”,函数中有一行"Application.DisplayAlerts = False" ,问题是如果我手动运行这个函数,它不会导致任何错误,错误"Method 'Display Alerts' of object '_Application Failed' error" Method'Display "Method 'Display Alerts' of object '_Application Failed' error"当我从中间层(C#)调用函数执行它时抛出"Method 'Display Alerts' of object '_Application Failed' error"会抛出。

我不知道要在这里检查。

请有人回复。

提前致谢。

我的猜测是,这与MS Internet Explorer有关(全文在这里 )。 在你的问题中,你并没有指定如何或从哪里调用你的macros,所以我不能确定。 但是,我已经看到这样的代码引用MSIE Application对象而不是Excel的实例。 即使它不是MSIE,它仍然可能是另一个有自己的Application定义的对象,因此它与Excel的Application对象冲突,所以下面的内容仍然适用。

如果我是正确的,则需要在更改DisplayAlerts属性的代码行之前将以下代码行添加到macros:

 On Error Resume Next 

您也可以在更改DisplayAlerts属性的代码行之后添加以下代码行:

 On Error GoTo 0 

所以基本上是这样的:

 On Error Resume Next 'This line is required. Application.DisplayAlerts = False On Error GoTo 0 'This line is optional to revert the error handling to default behaviour. 

通过将这些代码行添加到您的macros,您可以抑制错误消息,macros继续运行。 请注意,在这种情况下,您不能更改DisplayAlerts属性。