vba 将即时窗口的内容写入文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7015486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write contents of immediate window to a text file
提问by Shayon Saleh
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?
我正在编写一个通过文档并尝试按样式解析它的宏。现在,指定样式的任何内容都被复制到直接窗口中。有没有办法进一步自动化宏以将文本从直接窗口移动到 txt 文件中?否则,除非打开 VBA,否则任何使用宏的人都无法看到文本,对吗?
回答by Jean-Fran?ois Corbett
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.
请注意,最后一段代码需要设置一个引用:Microsoft Scripting Runtime 中的工具 > 引用 > 复选标记。
回答by Ravikanth K
Put this code to immediate window and hit enter to write the List to JSON text in C#.
将此代码放入立即窗口并按 Enter 键以将列表写入 C# 中的 JSON 文本。
System.IO.File.WriteAllText(@"C:\Users\m1028200\Desktop\Json2.txt",
JsonConvert.SerializeObject(resultsAll));
回答by Andreas Dietrich
I would recommend to use some best-practices-based logging framework like VBA Logging, which supports file logging or console configurably in parallel etc.
我建议使用一些基于最佳实践的日志记录框架,如VBA Logging,它支持文件日志记录或并行配置的控制台等。
example usage (e.g. in some Foo.bas
module):
示例用法(例如在某些Foo.bas
模块中):
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):
导致类似(在控制台和vba_logging.log
文件中):
(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