Word VBA 宏——将多个项目写入 txt 文件?

发布于 2024-09-17 16:55:35 字数 430 浏览 1 评论 0原文

我正在开发一个项目来存储用户在 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 技术交流群。

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

发布评论

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

评论(2

﹏雨一样淡蓝的深情 2024-09-24 16:55:35

附加到文件不是 For Output 而是 For Append

Open "C:\Temp\HoldPastes.txt" For Append As #1

读取剪贴板

Dim myData As DataObject
Dim strClip As String

Set myData = New DataObject
myData.GetFromClipboard
strClip = myData.GetText

希望这有帮助

Append to a file is not For Output but For Append

Open "C:\Temp\HoldPastes.txt" For Append As #1

Read the Clipboard

Dim myData As DataObject
Dim strClip As String

Set myData = New DataObject
myData.GetFromClipboard
strClip = myData.GetText

Hope this helps

ゞ记忆︶ㄣ 2024-09-24 16:55:35

而不是:
写#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.

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