E
Ed
I often do reports with lots of tables that get added, deleted and shuffled.
Tiring of going back through the document after each change to manually
renumber the tables, I created a macro to help me. (Unfortunately, due to
other considerations, the use of specific styles and fields aren't
possible.) The macro finds the selection point, sets a range to the
beginning of the document, and counts all the tables within the range. It
then adds one, puts it in the proper place, and we move on.
They made me use Track Changes on this round of reports, and I think it may
be messing with my macro. After successfully counting 35 tables, the next
one suddenly numbered 49!! I turned off Tracking and tried again, with the
same results. The macro is below. Any suggestions?
Ed
Sub CntTables()
Dim doc As Document
Dim rng1 As Range
Dim rng2 As Range
Set doc = ActiveDocument
' The table number placeholder is double-clicked
Set rng1 = Selection.Range
' Removing any trailing spaces from the range
Do While Right(rng1.Text, 1) = " "
rng1.MoveEnd wdCharacter, -1
Loop
Set rng2 = doc.Range(doc.Characters(1), rng1.End)
rng1.Text = rng2.Tables.Count + 1
End Sub
Tiring of going back through the document after each change to manually
renumber the tables, I created a macro to help me. (Unfortunately, due to
other considerations, the use of specific styles and fields aren't
possible.) The macro finds the selection point, sets a range to the
beginning of the document, and counts all the tables within the range. It
then adds one, puts it in the proper place, and we move on.
They made me use Track Changes on this round of reports, and I think it may
be messing with my macro. After successfully counting 35 tables, the next
one suddenly numbered 49!! I turned off Tracking and tried again, with the
same results. The macro is below. Any suggestions?
Ed
Sub CntTables()
Dim doc As Document
Dim rng1 As Range
Dim rng2 As Range
Set doc = ActiveDocument
' The table number placeholder is double-clicked
Set rng1 = Selection.Range
' Removing any trailing spaces from the range
Do While Right(rng1.Text, 1) = " "
rng1.MoveEnd wdCharacter, -1
Loop
Set rng2 = doc.Range(doc.Characters(1), rng1.End)
rng1.Text = rng2.Tables.Count + 1
End Sub