Word Macro to insert rows to multiple tables

Closed
Report
-
Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
-
Hello,





Using MS Word..

I have a few rows to be added to each table; and there are multiple tables.. These rows need to be added before the row with text in the first cell as 'Procedure'.
There are two columns and multiple rows in each table..

I am trying to use the following macro :

Sub copyn()
'macro to copy rows to multiple tables

Dim tTable As Table
Dim TargetText as String
Dim oRow as Row
TargetText = "Procedure"

For Each tTable In ActiveDocument.Tables
For Each oRow in tTable.Rows
If oRow.Cells(1).Range.Text = TargetText Then Selection.Paste
Next oRow
Next tTable

End Sub

However, nothing happens on running the macro :)

[I first record the macro to copy text to clipboard, which is Selection in this case, and then edit the macro. I need the same three rows to be pasted over and over]

Any suggestions for resolution would be greatly appreciated.. I have multiple documents, each with over hundred tables..

1 reply

Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
768
In word 2007, when I inserted a table and ran your code. this is what I found
the cell that contain "Procedure" also comes up with baggage. There are two extra non printable characters. So the match failed. In my case I had to make this change

Sub copyn()
'macro to copy rows to multiple tables

Dim tTable        As Table
Dim TargetText    As String
Dim oRow          As Row
Dim v             As Variant


   TargetText = "Procedure"
   
   For Each tTable In ActiveDocument.Tables
      For Each oRow In tTable.Rows
         v = oRow.Cells(1).Range.Text
         v = Replace(v, Chr(13), vbNullString)
         v = Replace(v, Chr(7), vbNullString)
         If v = TargetText Then Selection.Paste
      Next oRow
   Next tTable

End Sub