我正在编写一个遍历文档并尝试按样式解析它的宏。现在,指定样式的任何内容都将复制到即时窗口中。有没有办法进一步自动化宏以将文本从即时窗口移动到 txt 文件中?否则,除非打开 VBA,否则使用宏的任何人都无法看到文本,对吗?
问问题
15424 次
3 回答
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
请注意,最后一段代码需要设置一个引用:工具 > 引用 > Microsoft 脚本运行时的复选标记。
于 2011-08-11T07:10:44.700 回答
1
将此代码放到即时窗口中,然后按 Enter 以将列表写入 C# 中的 JSON 文本。
System.IO.File.WriteAllText(@"C:\Users\m1028200\Desktop\Json2.txt",
JsonConvert.SerializeObject(resultsAll));
于 2017-08-14T13:56:22.910 回答
0
我建议使用一些基于最佳实践的日志记录框架,例如VBA Logging,它支持文件日志记录或可并行配置的控制台等。
示例用法(例如在某些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
导致类似(在控制台和vba_logging.log
文件中):
(16.12.2018 13:08:30)[XlsxMgr::MySub]-INFO: This is my message ..
日志配置文件如下所示:
## vba_logging.properties
LOG_LEVEL = info
LOG_TO_CONSOLE = True
LOG_TO_BUFFER = True
于 2018-12-16T12:24:29.623 回答