Examples for preparing Word documents using macros
Applying the macros
- Open the document to convert in Word
- Remove the unnecessary parts (revision history, table of contents, etc) from the document
- Click on Developer tab in horizontal top toolbar
- Click on Macros
- Click on New
- In the upcoming window, paste the macro text from here
- Click on the Run symbol - the conversion will happen
- Save the converted document under another file name
Replacing table rows with a heading (imported as summary) and table row content
This preparation is usually used when
- The items in the document are stored in many tables
- The table cells are not merged vertically
- The user wants to do semantic review after importing the items
The macro will separate the table row contents vertically, and will insert headings with uniform text before each table row content. The table will be removed, and only the row contents will be preserved.
Here is the macro text:
Sub AllTablesToHeadings()
'
' AllTablesToHeadings Macro
' Macro created by Jaroslaw Michalak, Sandor Szabo
'
For Each aTable In ActiveDocument.Tables
For Each oRow In aTable.Rows
oRow.Cells(1).Select
With Selection
.InsertParagraphBefore
.InsertBefore "Requirement converted from table cell "
End With
Next oRow
Next aTable
For Each aTable In ActiveDocument.Tables
aTable.ConvertToText wdSeparateByCommas, True
Next aTable
With ActiveDocument.Content.Find
.ClearFormatting
.Text = "Requirement converted from table cell"
With .Replacement
.ClearFormatting
.Style = ActiveDocument.Styles("Heading 3")
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceAll
End With
End Sub
Replacing table rows with text separated vertically
This preparation is usually used when
- The items in the document are stored in many tables
- The table cells are not merged vertically
- The user wants to do semantic review before importing the items
The macro will remove tables, and keep only table row content, separated with many new lines. This way the user can review the individual texts of the former rows, and can give a title to each. Then the user formats the title as a heading. The heading level can be used to form a parent child structure.
Here is the macro text:
Sub AllTablesToHeadings()
'
' AllTablesToHeadings Macro
'
'
On Error Resume Next
For Each aTable In ActiveDocument.Tables
For Each oRow In aTable.Rows
oRow.Cells(1).Select
With Selection
.Delete
.InsertBefore vbCr
.InsertBefore vbCr
End With
Next oRow
Next aTable
For Each aTable In ActiveDocument.Tables
aTable.ConvertToText wdSeparateByCommas, True
Next aTable
End Sub