Write contents of immediate window to a text file Write contents of immediate window to a text file vba vba

Write contents of immediate window to a text file


Here's my suggestion: write to the immediate window AND to a file at the same time. Examples below.

Why make the information first transit in the immediate window, and only then write it to a file from there? That just sounds perversely and uselessly difficult!

Dim s As StringDim n As Integern = FreeFile()Open "C:\test.txt" For Output As #ns = "Hello, world!"Debug.Print s ' write to immediatePrint #n, s ' write to files = "Long time no see."Debug.Print sWrite #n, s ' other way of writing to fileClose #nDim FSO As Scripting.FileSystemObjectSet FSO = New Scripting.FileSystemObjectDim txs As Scripting.TextStreamSet txs = FSO.CreateTextFile("C:\test2.txt")s = "I like chickpeas."Debug.Print s ' still writing to immediatetxs.WriteLine s ' third way of writing to filetxs.CloseSet txs = NothingSet FSO = Nothing

Note that this last bit of code requires a reference to be set: Tools > References > checkmark at Microsoft Scripting Runtime.


Put this code to immediate window and hit enter to write the List to JSON text in C#.

System.IO.File.WriteAllText(@"C:\Users\m1028200\Desktop\Json2.txt", JsonConvert.SerializeObject(resultsAll));


I would recommend to use some best-practices-based logging framework like VBA Logging, which supports file logging or console configurably in parallel etc.

example usage (e.g. in some Foo.bas module):

Sub MySub()  Logging.setModulName (Application.VBE.ActiveVBProject.Name)  Set log = Logging.getNewLogger(Application.VBE.ActiveVBProject.Name)  Call log.setLoggigParams(Logging.lgALL, True, True, True) ' log  ALL to Console, Buffer, File  log.logINFO "This is my message ..", "MySub"End Sub

resulting in something like (both in console and vba_logging.log file):

(16.12.2018 13:08:30)[XlsxMgr::MySub]-INFO:  This is my message ..

where the log config file looks like this:

## vba_logging.propertiesLOG_LEVEL = infoLOG_TO_CONSOLE = TrueLOG_TO_BUFFER = True