How can I open a console window from Excel?
I want to display debug information.
I already log to a UserForm, the Immediate window and files.
I tried using AllocConsole
, but it fails with error 87 : The parameter is incorrect
.
Option Explicit
Private Declare PtrSafe Function FreeConsole Lib "kernel32" () As Long
Private Declare PtrSafe Function AllocConsole Lib "kernel32" () As Long
'https://learn.microsoft.com/en-us/windows/console/allocconsole
Sub Example()
Dim l As Long
l = FreeConsole
Debug.Print "FreeConsole: " & l ' 1
If FreeConsole = 0 Then
Debug.Print Err.LastDllError, "FreeConsole failed"
Exit Sub
End If
l = AllocConsole
Debug.Print "AllocConsole: " & l ' 0
If l = 0 Then
Debug.Print Err.LastDllError ' 87 : The parameter is incorrect.
Exit Sub
End If
End Sub
Output:
FreeConsole: 1
AllocConsole: 0
87
N.B. I'm using Excel 365 (Version 2207 Build 1547.20284) on Windows 10 (20H2).