通过 COM 互操作从 C# 读取单元格值期间忽略 excel vba 错误

发布于 2024-09-13 14:36:55 字数 103 浏览 3 评论 0原文

我正在从 C# 运行 VBA 宏,这可能会产生错误。这些错误的特点是弹出调试提示、中断应用程序并要求用户输入。我需要这些宏,但无法禁用它们。

如何忽略这些错误或自动关闭对话框?

I am running VBA macros from C# which can yield errors. These errors are characterized by a pop-up prompting for debugging, interrupting the application and requiring user input. I need these macros and they cannot be disabled.

How I can ignore these errors or close dialogs automatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆那么伤 2024-09-20 14:36:55

或者以最简单最简单的方式。在宏中包含on error resume next,这样宏就不会因错误而停止。

Ör in the most simplest easiest way. Include on error resume next in your macros,so that the macro wont stop at errors.

疧_╮線 2024-09-20 14:36:55

要在尝试打开文件之前隐藏 Visual Basic 编辑器(发生错误时显示),请将 Excel.VBE.MainWindow.Visible 更改为 false。请注意,如果未显示调试器,您将必须捕获代码中的异常,因此,请使用 try catch 封装您的代码。

如果不需要并且可以忽略宏,请使用 msoAutomationSecurityForceDisable 完全禁用它们。

using InteropExcel = Microsoft.Office.Interop.Excel;

var Excel = new InteropExcel.Application ();

// Excel window will NOT popup if macro errors are found but
// exceptions might be raised when you execute the broken
// macro.
Excel.VBE.MainWindow.Visible = false;

// Uncomment to disable macros completely.
// Excel.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;

// I used this snippet to open a file with a broken macro.
var MyWorkbook = Excel.Workbooks.Open (@"YourFilePath");
var FirstWorksheet = (InteropExcel.Worksheet)MyWorkbook.Worksheets ["Plan1"];
var MyCell = ((InteropExcel.Range)FirstWorksheet.Cells [1,1]);
var CellValue = (Int32)MyCell.Value;

To hide the visual basic editor (which shows when a error occurs) before attempting to open the file, change the Excel.VBE.MainWindow.Visible to false. Be aware that if the debugger is not shown, you will have to catch an exception in your code, so, envelope your code with an try catch.

If macros are not needed and can be ignored, use the msoAutomationSecurityForceDisable to disable them completely.

using InteropExcel = Microsoft.Office.Interop.Excel;

var Excel = new InteropExcel.Application ();

// Excel window will NOT popup if macro errors are found but
// exceptions might be raised when you execute the broken
// macro.
Excel.VBE.MainWindow.Visible = false;

// Uncomment to disable macros completely.
// Excel.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;

// I used this snippet to open a file with a broken macro.
var MyWorkbook = Excel.Workbooks.Open (@"YourFilePath");
var FirstWorksheet = (InteropExcel.Worksheet)MyWorkbook.Worksheets ["Plan1"];
var MyCell = ((InteropExcel.Range)FirstWorksheet.Cells [1,1]);
var CellValue = (Int32)MyCell.Value;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文