Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write to the console from Excel vba code?

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?

like image 710
froadie Avatar asked Jan 26 '10 20:01

froadie


People also ask

How do I write text code in VBA?

To create a text using a VBA code, you need to use the CreateTextFile method. This method allows you to define a location where you want to create it. This method has a syntax that where you can specify if you want to overwrite the file on the location and specify whether the file is created as a Unicode or ASCII file.

Does VBA have a console?

The VBA console can be used in many ways from basic mathematical operations to testing lines of codes.

How do I open the VBA console in Excel?

For accessing VBA Editor, Press Alt + F11 keys together. This simple shortcut is easy to remember and quite frequently used when we regularly work on VBA Coding. This shortcut key can be used both ways to get into VBA Editor Window and from the VBA Editor window to Excel.


1 Answers

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!

like image 147
froadie Avatar answered Oct 15 '22 02:10

froadie