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

Re: Macro gives wrong table count? by Jean-Guy

Jean-Guy
Mon Aug 01 21:26:14 CDT 2005

Ed was telling us:
Ed nous racontait que :

> 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?

Have you accepted changes as well (Not just turn off Track changes)?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org




Re: Macro gives wrong table count? by Ed

Ed
Tue Aug 02 10:16:00 CDT 2005

Hello, Jean-Guy.

> > 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.
>
> Have you accepted changes as well (Not just turn off Track changes)?

No, I did not. I thought the macro might also be counting tables in the
changed portions. The changes are being tracked so the "higher-ups" can
pass judgment on my editing. If I accept any change, it's not available for
them to see, right? If that's the case, then I either need a VBA method to
check if the table is in an unaccepted change, or stop when I see an error
and continue manually. Or maybe put this work effort into getting an
autocount field in the Table headers!

Ed



Re: Macro gives wrong table count? by Jean-Guy

Jean-Guy
Tue Aug 02 12:31:25 CDT 2005

Ed was telling us:
Ed nous racontait que :

> Hello, Jean-Guy.
>
>>> 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.
>>
>> Have you accepted changes as well (Not just turn off Track changes)?
>
> No, I did not. I thought the macro might also be counting tables in
> the changed portions. The changes are being tracked so the
> "higher-ups" can pass judgment on my editing. If I accept any
> change, it's not available for them to see, right? If that's the
> case, then I either need a VBA method to check if the table is in an
> unaccepted change, or stop when I see an error and continue manually.
> Or maybe put this work effort into getting an autocount field in the
> Table headers!

Here is some code that may help you get going. It checks every table to see
if a changes has been made.

One problem is if the user selected more than one table to be deleted in one
operation.This code will return only the first table.

To get around that, you would need to iterate the revisions collection
instead.

'_______________________________________
Dim TableCountlng As Long
Dim i As Long
Dim j As Long
Dim myTable As Table

TableCountlng = ActiveDocument.Tables.Count

For i = 1 To TableCountlng
Set myTable = ActiveDocument.Tables(i)
With myTable
If .Range.Revisions.Count > 0 Then
For j = 1 To .Range.Revisions.Count
If .Range.Revisions(j).Type = wdRevisionDelete Then
If .Range.InRange(.Range.Revisions(j).Range) Then
MsgBox "Table number #" & i & " is marked to be
deleted"
End If
End If
Next
End If
End With
Next
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org




Re: Macro gives wrong table count? by Ed

Ed
Tue Aug 02 14:12:35 CDT 2005

Jean-Guy: Thank you for helping me with the Revisions. InRange is a pretty
neat trick, too. Unfortunately, my document so far has over 900 revisions!
To iterate through each one was taking a l-o-o-n-g time! So I used a piece
of code that sets a range to just the page I'm on, and iterated through the
revisions in just that range just to make sure I have all these concepts
right. Much quicker - but it doesn't catch it!

Showing the revisions, I selected a deleted word. The macro below sets a
range to the selection, sets a range to the page, checks each revision for
Type Deletion, and if it's Deletion checks if the selection range is in the
revision range. All variables set correctly. The selection is in
Revision(5). The macro runs right over it and ends with no MsgBox! Can you
see what I've got wrong?
Ed

Sub Foo_GetMyInfo()

Dim doc As Document
Set doc = ActiveDocument
Dim rng As Range
Set rng = Selection.Range
Dim y As Long
Dim rev As Revision
Dim Pagerange As Range
Set Pagerange = _
doc.Range(doc.Bookmarks("\page").Range.Start, _
doc.Bookmarks("\page").Range.End - 1)

For y = 1 To Pagerange.Revisions.Count
Set rev = Pagerange.Revisions(y)
If rev.Type = wdRevisionDelete Then
If rng.InRange(doc.Revisions(y).Range) = True Then
MsgBox "Revised"
End If
End If
Next y

End Sub



Re: Macro gives wrong table count? by Jean-Guy

Jean-Guy
Tue Aug 02 14:44:16 CDT 2005

Ed was telling us:
Ed nous racontait que :

> Jean-Guy: Thank you for helping me with the Revisions. InRange is a
> pretty neat trick, too. Unfortunately, my document so far has over
> 900 revisions! To iterate through each one was taking a l-o-o-n-g
> time! So I used a piece of code that sets a range to just the page
> I'm on, and iterated through the revisions in just that range just to
> make sure I have all these concepts right. Much quicker - but it
> doesn't catch it!
>
> Showing the revisions, I selected a deleted word. The macro below
> sets a range to the selection, sets a range to the page, checks each
> revision for Type Deletion, and if it's Deletion checks if the
> selection range is in the revision range. All variables set
> correctly. The selection is in Revision(5). The macro runs right
> over it and ends with no MsgBox! Can you see what I've got wrong?
> Ed
>
> Sub Foo_GetMyInfo()
>
> Dim doc As Document
> Set doc = ActiveDocument
> Dim rng As Range
> Set rng = Selection.Range
> Dim y As Long
> Dim rev As Revision
> Dim Pagerange As Range
> Set Pagerange = _
> doc.Range(doc.Bookmarks("\page").Range.Start, _
> doc.Bookmarks("\page").Range.End - 1)
>
> For y = 1 To Pagerange.Revisions.Count
> Set rev = Pagerange.Revisions(y)
> If rev.Type = wdRevisionDelete Then
> If rng.InRange(doc.Revisions(y).Range) = True Then
> MsgBox "Revised"
> End If
> End If
> Next y
>
> End Sub


You won't believe this...

Try this line
If rng.InRange(rev.Range) Then
instead of
If rng.InRange(doc.Revisions(y).Range) = True Then

rev.Range represent the actual revision the loop is currently testing,
whereas doc.Revisions(y) represents the document's y'th revisions, which may
not be the same as rev, unless you are on the first page...!

Now, go ahead, and pull your hair!


Note:
I removed the "= True " from the If statement because InRange returns a
Boolean already, so by itself, it is already either true or false.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org




Re: Macro gives wrong table count? by Ed

Ed
Tue Aug 02 16:01:32 CDT 2005

> Try this line
> If rng.InRange(rev.Range) Then
If you were here or I were there, I'd come over and let you smack me!! It
works great!

Thank you for saving me much time and hair!
Ed



Re: Macro gives wrong table count? by Jean-Guy

Jean-Guy
Tue Aug 02 16:39:38 CDT 2005

Ed was telling us:
Ed nous racontait que :

>> Try this line
>> If rng.InRange(rev.Range) Then
> If you were here or I were there, I'd come over and let you smack
> me!! It works great!
>
> Thank you for saving me much time and hair!
> Ed

lol
Glad I could help!
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org