Reputation: 51
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
Upvotes: 5
Views: 2573
Reputation: 16899
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
Upvotes: 3