如何从 Excel vba 代码写入控制台?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2142295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I write to the console from Excel vba code?
提问by froadie
Is there any way to print a message to the console from an Excel vba macro? I have a small VB .NET console application that calls an excel macro, and I'd like the error messages from the macro code to print to the console. Debug.print doesn't help as it only prints to the immediate window. Any ideas?
有没有办法从 Excel vba 宏向控制台打印消息?我有一个调用 excel 宏的小型 VB .NET 控制台应用程序,我希望将宏代码中的错误消息打印到控制台。Debug.print 没有帮助,因为它只打印到即时窗口。有任何想法吗?
采纳答案by froadie
I found that there's no communication between a VB .NET project and a macro that it calls - the macro can't access the VB's console, and it can't throw back an exception. If an error occurs in the macro, it's not detected by the VB .NET code. The VB project just calls the macro, it executes, and then the project continues running, with no knowledge of what happened in the macro. The way I got around this was that I had the macro write the error to a specific designated cell in the Excel spreadsheet, and then the VB project read that and used it to determine if there was an error, and then would write it to the console if there was. Sort of a cheat fix, but it worked. Thanks for all the suggestions!
我发现 VB .NET 项目和它调用的宏之间没有通信——宏不能访问 VB 的控制台,也不能抛出异常。如果宏中出现错误,则 VB .NET 代码不会检测到该错误。VB 项目只是调用宏,它执行,然后项目继续运行,不知道宏中发生了什么。我解决这个问题的方法是让宏将错误写入 Excel 电子表格中的特定指定单元格,然后 VB 项目读取并使用它来确定是否有错误,然后将其写入控制台如果有。有点作弊修复,但它奏效了。感谢所有的建议!
回答by tree
I had to use Debug.Print() as Debug.Write() and Console.WriteLine() did not work
我不得不使用 Debug.Print() 作为 Debug.Write() 和 Console.WriteLine() 不起作用
回答by Phil.Wheeler
You can try using Debug.WriteLine()to output your debugging information to the Visual Studio Output window, but I have the nagging feeling you can't do that unless you're writing a Console application.
您可以尝试使用Debug.WriteLine()将您的调试信息输出到 Visual Studio 输出窗口,但我有一种唠叨的感觉,除非您正在编写控制台应用程序,否则您无法这样做。
If it indeed doesn't work, try using the Console.SetOut()method to redirect the Console.WriteLine()strings to a StreamWriter, which could be a file on your machine. Not the ideal, but possibly a workable solution?
如果确实不起作用,请尝试使用该Console.SetOut()方法将Console.WriteLine()字符串重定向到 StreamWriter,它可能是您机器上的一个文件。不是理想的,但可能是一个可行的解决方案?
回答by ChaosPandion
Pass it a file path and write all the errors to the file.
将文件路径传递给它并将所有错误写入文件。
回答by burnside
You could create a COM component in your VB.NET app that you can call from VBA and write to your application's console.
您可以在 VB.NET 应用程序中创建一个 COM 组件,您可以从 VBA 调用该组件并写入应用程序的控制台。
回答by MarkJ
Why don't you return a string containing the error messages from the macro to the .Net code, and have the .Net code write it to the console?
为什么不将包含错误消息的字符串从宏返回到 .Net 代码,并让 .Net 代码将其写入控制台?
Alternatively, if these are really errors, raise errors from the macro using Err.Raiseand handle them in the .Net code with a Try Catchblock that writes the details to the console.
或者,如果这些确实是错误,请使用宏从宏中引发错误Err.Raise并在 .Net 代码中使用Try Catch将详细信息写入控制台的块处理它们。

