Home |
Search |
Today's Posts |
#1
|
|||
|
|||
Macro gives wrong table count?
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 |
#2
|
|||
|
|||
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 ISTOO Word MVP site: http://www.word.mvps.org |
#3
|
|||
|
|||
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 |
#4
|
|||
|
|||
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 ISTOO Word MVP site: http://www.word.mvps.org |
#5
|
|||
|
|||
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 |
#6
|
|||
|
|||
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 ISTOO Word MVP site: http://www.word.mvps.org |
#7
|
|||
|
|||
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 |
#8
|
|||
|
|||
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 ISTOO Word MVP site: http://www.word.mvps.org |
Reply |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Table in a Form | Tables | |||
Select specific cells in table via macro | Microsoft Word Help | |||
Table AutoFormats vs. Table Styles confusion | Tables | |||
Ideas for macro to add row for count and percentage | Tables | |||
Word 2003 Table AutoFormat vs Macro vs VBA | Tables |