我正在编写一段宏代码,该代码通过样式遍历文档并尝试解析它。目前,指定样式中的任何内容都会被复制到立即窗口中。是否有一种方法可以进一步自动化宏来将文本从立即窗口移动到txt文件中?否则,使用该宏的任何人都无法查看文本,除非他们打开VBA,对吗?
我正在编写一段宏代码,该代码通过样式遍历文档并尝试解析它。目前,指定样式中的任何内容都会被复制到立即窗口中。是否有一种方法可以进一步自动化宏来将文本从立即窗口移动到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。
FileSystemObject
方法。 - Jean-François CorbettSystem.IO.File.WriteAllText(@"C:\Users\m1028200\Desktop\Json2.txt",
JsonConvert.SerializeObject(resultsAll));
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