Write contents of immediate window to a text file
Asked Answered
T

3

12

I'm writing a macro which goes through a document and tries to parse it by Style. Right now, anything in the designated style is copied onto the immediate window. Is there a way to automate the macro further to move the text from the immediate window into a txt file? Otherwise, anyone using the macro would not be able to see the text unless they opened up VBA, correct?

Tillietillinger answered 10/8, 2011 at 17:55 Comment(3)
How are you putting it into the immediate window? Can you store it into a string variable instead and drop it into a text file when the process is completed?Pimiento
... or just pop it up in a form.Demmy
I don't understand why you would want to have the information first transit in the immediate window, and only then be written to a file from there. Why not write to file directly??Asti
C
20

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 String
Dim n As Integer

n = FreeFile()
Open "C:\test.txt" For Output As #n

s = "Hello, world!"
Debug.Print s ' write to immediate
Print #n, s ' write to file

s = "Long time no see."
Debug.Print s
Write #n, s ' other way of writing to file

Close #n


Dim FSO As Scripting.FileSystemObject
Set FSO = New Scripting.FileSystemObject
Dim txs As Scripting.TextStream
Set txs = FSO.CreateTextFile("C:\test2.txt")
s = "I like chickpeas."
Debug.Print s ' still writing to immediate
txs.WriteLine s ' third way of writing to file
txs.Close
Set txs = Nothing
Set FSO = Nothing

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

Cruel answered 11/8, 2011 at 7:10 Comment(2)
Hello, thanks for the answer! I have a question: is there any advantage/inconvenient in using one of the methods instead of another?Applegate
Yes. I suggest reading up on them and trying them out. Personally, I almost always use the FileSystemObject method.Asti
S
1

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));
Swayne answered 14/8, 2017 at 13:56 Comment(1)
This is a VBA question.Karlise
E
0

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.properties
LOG_LEVEL = info
LOG_TO_CONSOLE = True
LOG_TO_BUFFER = True
Eyeglass answered 16/12, 2018 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.