Ignore excel vba errors while reading cell values ​​from C # through COM interaction

I am running VBA macros from C #, which can lead to errors. These errors are characterized by a tooltip for debugging, interruption of the application, and user input. I need these macros and they cannot be disabled.

How can I ignore these errors or close dialogs automatically?

+6
c # excel-vba com-interop
source share
2 answers

Γ–r in the easiest way. Include on error resume next in your macros so that the macro does not stop when errors occur.

+1
source share

To hide the visual base editor (which shows when an error occurs), before trying to open the file, change the value of Excel.VBE.MainWindow.Visible to false. Keep in mind that if the debugger does not appear, you will have to catch the exception in your code, so convert your code with try catch.

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

 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; 
0
source share

All Articles