Word Macro to insert rows to multiple tables

Closed
such - May 16, 2011 at 02:44 PM
rizvisa1 Posts 4479 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 - May 24, 2011 at 10:26 AM
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

rizvisa1 Posts 4479 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 768
May 24, 2011 at 10:26 AM
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
0