Word VBA 宏——将多个项目写入 txt 文件?
我正在开发一个项目来存储用户在 Word 文档中剪切/复制/粘贴的内容,并使用 VBA 宏来完成此任务。以下是粘贴宏的片段:
Open "C:\Temp\HoldPastes.txt" For Output As #1
Write #1, "TestTestTest."
Write #1, Selection
Close #1
我希望 HoldPastes.txt 包含用户粘贴的每个文本块的列表。
首先,Write #1, Selection
是错误的;它在我的txt文件中添加了两个引号。如何访问从剪贴板粘贴的内容并将其写入我的文件?
另外,这会覆盖 HoldPastes.txt 中的所有内容。我想保留此文件中的所有粘贴,那么如何告诉宏从上次中断的位置开始并将其添加到文件中?
I'm working on a project to store what a user cuts/copies/pastes within a Word document, and am using the VBA macros to accomplish this. Here's a snippet from the paste macro:
Open "C:\Temp\HoldPastes.txt" For Output As #1
Write #1, "TestTestTest."
Write #1, Selection
Close #1
I'd like HoldPastes.txt to have a list of every chunk of text the user has pasted.
First off, Write #1, Selection
is wrong; it puts two quotation marks into my txt file. How can I access what's been pasted from the clipboard and write that to my file?
Also, this overwrites whatever I had in HoldPastes.txt. I'd like to preserve all pastes in this file, so how can I tell the macro to pick up where it left off and add to the file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
附加到文件不是
For Output
而是For Append
读取剪贴板
希望这有帮助
Append to a file is not
For Output
butFor Append
Read the Clipboard
Hope this helps
而不是:
写#1,“文本”
你需要使用:
print #1, "text"
写入命令将始终在保存到文件的数据周围使用 " 标记。
instead of:
write #1, "text"
you need to use:
print #1, "text"
the write command will always use " marks around the data being saved to the file.