将立即窗口的内容写入文本文件

Sha*_*leh 11 vba immediate-window

我正在编写一个宏,它通过一个文档并试图通过Style解析它.现在,指定样式的任何内容都会复制到即时窗口中.有没有办法自动化宏进一步将文本从即时窗口移动到txt文件?否则,使用宏的任何人都无法看到文本,除非他们打开VBA,对吗?

Jea*_*ett 20

这是我的建议:同时写入即时窗口和文件.以下示例.

为什么首先在即时窗口中传输信息,然后才将其写入文件?这听起来很反常而且毫无用处!

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
Run Code Online (Sandbox Code Playgroud)

请注意,最后一段代码需要设置引用:Microsoft Scripting Runtime中的工具>引用>复选标记.