将立即窗口的内容写入文本文件

发布于 2024-11-28 22:47:50 字数 120 浏览 0 评论 0原文

我正在编写一个宏,它会遍历文档并尝试按样式解析它。现在,指定样式的任何内容都会复制到直接窗口中。有没有办法进一步自动化宏,将文本从即时窗口移动到 txt 文件中?否则,任何使用宏的人都无法看到文本,除非他们打开 VBA,对吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野鹿林 2024-12-05 22:47:50

这是我的建议:同时写入即时窗口和文件。下面的例子。

为什么要让信息首先在立即窗口中传输,然后才从那里写入文件?这听起来很反常且毫无用处!

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 处的复选标记。

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.

—━☆沉默づ 2024-12-05 22:47:50

将此代码放入即时窗口并按 Enter 键以将列表写入 C# 中的 JSON 文本。

System.IO.File.WriteAllText(@"C:\Users\m1028200\Desktop\Json2.txt", 
JsonConvert.SerializeObject(resultsAll));

Put this code to immediate window and hit enter to write the List to JSON text in C#.

System.IO.File.WriteAllText(@"C:\Users\m1028200\Desktop\Json2.txt", 
JsonConvert.SerializeObject(resultsAll));
寄意 2024-12-05 22:47:50

我建议使用一些基于最佳实践的日志框架,例如VBA 日志记录,支持并行配置的文件日志记录或控制台等

示例用法(例如在某些 Foo.bas module):

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

I would recommend to use some best-practices-based logging framework like VBA Logging, which supports file logging or console configurably in parallel etc.

example usage (e.g. in some Foo.bas module):

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):

(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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文