我正在编写一个通过文档并尝试按样式解析它的宏。现在,指定样式的任何内容都被复制到直接窗口中。有没有办法进一步自动化宏以将文本从直接窗口移动到 txt 文件中?否则,除非打开 VBA,否则任何使用宏的人都无法看到文本,对吗?
最佳答案
这是我的建议:同时写入即时窗口和文件。下面的例子。
为什么要先在即时窗口中传输信息,然后才从那里将其写入文件?这听起来很反常和无用的困难!
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
请注意,最后一段代码需要设置一个引用:Microsoft Scripting Runtime 中的工具 > 引用 > 复选标记。
关于vba - 将即时窗口的内容写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7015486/