Repeating for each table in a document

S

stevewy

I have written a short macro (pasted in below) that works on the
current table that the cursor is in, but I want it to run on all the
tables (except the very first one) in the document in one go. I tried
using For Each but the macro keeps bringing up errors, presumably
because there needs to be some alterations of the
"Selection.Tables(1)" bit where it occurs. Or is there something else
wrong? Any help would be appreciated. Thanks.


Sub Staff_Survey_Processing()
Selection.Find.ClearFormatting
With Selection.Find
.text = "Housing and Community Development"
.Replacement.text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.SelectRow
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Cut

With Selection.Tables(1)
.Cell(4, 1).Select
Selection.SelectRow
Selection.MoveDown Unit:=wdLine, Count:=26, Extend:=wdExtend
Selection.Rows.Delete

Selection.PasteAppendTable
.TopPadding = InchesToPoints(0.02)
.BottomPadding = InchesToPoints(0.02)
.LeftPadding = InchesToPoints(0.08)
.RightPadding = InchesToPoints(0.08)
.Spacing = 0
.Cell(4, 1).Select
Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter
Selection.Collapse
End With
End Sub


Steve Wylie
Canterbury
Kent
UK
 
D

Doug Robbins - Word MVP

I guess it is this part of your routine that you want to repeat

With Selection.Tables(1)
.Cell(4, 1).Select
Selection.SelectRow
Selection.MoveDown Unit:=wdLine, Count:=26, Extend:=wdExtend
Selection.Rows.Delete

You could do it as follows

Dim i as Long
With ActiveDocument
For i = 2 to .Tables.Count
With Tables(i)
.Cell(4, 1).Select
.etc
End With
Next i
End With

It would however be better to avoid using the Selection, but I have not
worked out exactly what your code is doing to advise you exatly what the
replacement code should be.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

stevewy

Yes, my coding is a bit of a mess, and it's difficult to explain what
I'm doing in a newsgroup message, where I cannot show the Word
document itself to give people a better idea!

However, your solution works a treat, and does exactly what I want it
to do. I'll hang on to that basic structure for future use if I need
to perform the same operation on multiple tables in a document.
From looking on the internet, and newsgroups, it appears there are
limitations with using Selection in some cases, and using Range is
preferred. However, I have not figured out how to use these commands
properly yet, so am cobbling together the subroutines as best I can.

Thanks for your assistance.

Steve Wylie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top