Open Excel file from batch script and return message to batch script after closing it again

时间:2015-06-26 10:11:43

标签: vba excel-vba excel

I open an Excel file from the command line, using statement like this in batch file:

start /wait excel "myWorkbook.xlsb" /e/parameters

Inside the workbook, there is an auto-open macro which does something. In case of any errors I want to close Excel and send an error message back to the batch file. How to achieve that? Is it possible to quit the application with an error code or something like this?

1 个答案:

答案 0 :(得分:1)

我认为你不能从VBA返回实际的错误代码,而是返回批处理文件。

如@Fabrizio所述,您可以向VBA添加错误处理代码。如果希望批处理文件检查Excel是否成功,则让VBA错误处理程序创建一个包含错误详细信息的文本文件,并让批处理文件检查文件是否存在。如果文件存在,则批处理文件可以重命名它。通过这种方式,您还可以记录错误。