打开受密码保护的Excel文件时禁用密码对话框

我有很多Excel文件受一些已知的密码保护。 我想以编程方式解除所有保护

我的问题是, 当我打开一个文件,使用一些错误的密码,它显示密码对话框,并等待用户inputDisplayAlerts = false; 不起作用。

如果我的密码错误,如何避免密码对话框 ? 有什么build议么?

我目前的代码:

using Excel = Microsoft.Office.Interop.Excel; bool TryUnprotectFile(string filePath, string trialPassword) { Excel.Application excelApp = new Excel.Application(); excelApp.DisplayAlerts = false; // when password is wrong, then it waits here for user input: Excel.Workbook wb = excelApp.Open(filePath, Password: trialPassword, IgnoreReadOnlyRecommended: true, Notify: false); // ... further unprotecting code ... // should return true when success, false otherwise }