从多个 Microsoft Word 文件中提取文本框数据

发布于 2024-12-13 21:27:02 字数 328 浏览 2 评论 0原文

我有一个文件夹,里面有 40-50 个 Microsoft Word 文档。

每个Word文档的结构如下: - 有多个表有两列 - 第二列包含问题,每个问题末尾有一个唯一标识符(例如“<#Q123#>”) - 在包含问题的同一单元格中,唯一标识符后面有一个文本框,其中包含问题的答案

我正在尝试在 Microsoft Word 中开发一个宏,它将打开文件夹中的每个 Word 文档,搜索文本框,然后将其粘贴到包含宏的新文件中。

如果可以将文本框中的数据提取到 Excel 文件中,而不是将文本框复制并粘贴到 Word 文档中,那就更好了,但我不确定这是否可能(因为某些文本框可能包含

I have a folder with 40-50 Microsoft Word documents.

Each Word document is structured as follows:
- There are multiple tables with two columns
- The second column contains the question, and at the end of each question there is a unique identifier (such as "<#Q123#>")
- In the same cell that contains the question, there is a text box after the unique identifier which contains the answer to the question

I am trying to develop a macro in Microsoft Word that will open each Word document in the folder, search for the text box that is immediately after the specified unique identifier, and paste it into the new file that has the macro.

If it were possible to extract the data from within the text boxes into an Excel file rather then copying and pasting the text boxes into a Word document, that would be preferrable, but I wasn't sure if that were possible (since some of the text boxes might contain

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

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

发布评论

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

评论(1

冬天旳寂寞 2024-12-20 21:27:02

列出的答案这里此处 可以向您展示如何获取文件夹中的所有文件。

以下 VBA 代码可用于检索表第二列中的文本以及同一单元格中形状(文本框)中的文本。如果在表格单元格中找不到形状(文本框),则使用 On Error Resume Next 语句来保持代码运行。

Dim cll As Cell
Dim question As String
Dim answer As String
Dim tbl As Table

'Gets the first table in the active document.
'In your code you would assign the Word document that you have
'just opened to a document variable.
Set tbl = ActiveDocument.Tables(1)

On Error Resume Next
For i = 1 To tbl.Rows.Count
    Set cll = tbl.Cell(i, 2)

    question = cll.Range.Text
    answer = cll.Range.ShapeRange.TextFrame.TextRange.Text

    'Once you have these two strings, you can do whatever you want with them.        
Next

The answers listed here and here can show you how to get all the files in your folder.

The following VBA code can be used to retrieve the text in the 2nd column of the table, as well as the text in the shape(textbox) that is in that same cell. The On Error Resume Next statement is in place to keep the code running if no shape(textbox) is found in the table cell.

Dim cll As Cell
Dim question As String
Dim answer As String
Dim tbl As Table

'Gets the first table in the active document.
'In your code you would assign the Word document that you have
'just opened to a document variable.
Set tbl = ActiveDocument.Tables(1)

On Error Resume Next
For i = 1 To tbl.Rows.Count
    Set cll = tbl.Cell(i, 2)

    question = cll.Range.Text
    answer = cll.Range.ShapeRange.TextFrame.TextRange.Text

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