vba - 将即时窗口的内容写入文本文件

标签 vba immediate-window

我正在编写一个通过文档并尝试按样式解析它的宏。现在,指定样式的任何内容都被复制到直接窗口中。有没有办法进一步自动化宏以将文本从直接窗口移动到 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/

相关文章:

vba - 问题 '381' 与范围 VBA

c# - 如何评估即时窗口中未处理的异常属性

c# - 如何在 visual studio 的即时窗口中运行 foreach 循环?

c# - VS 2017即时窗口显示 "Internal error in the C# compiler"

excel - dylib 动态库调用 dylib : Undefined symbols for architecture i386

excel - 基于单元格值动态隐藏列

vba - 如何在不更改公式的情况下更改单元格的文本?

c# - 如何在 VS 2013 的立即窗口中执行 LINQ 和/或 foreach?

excel - 复制/导出多列