Reputation: 69
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?
Upvotes: 6
Views: 1733
Reputation: 44
Ör in the most simplest easiest way. Include on error resume next
in your macros,so that the macro wont stop at errors.
Upvotes: 1
Reputation: 1626
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;
Upvotes: 0