setup
i'm working on a CSR and looking at a table that's several pages long, and now i want doublecheck something in the next table or, i forgot, maybe the one after that. i don't want to waste my life scrolling the mouse down 200 times. instead, i need ... a macro that helps me jump to the next table by one click of the mouse.
solution
the plan
- check if the current table is the last in the doc.
- if yes, prompt. if no, go forward to right after the current table, and keep going to the next paragraph until the cursor's inside a table.
programming language & module(s)
- VBA
file preps
make the doc to be processed the active document, i.e. the one where the cursor currently is in.
variables to customize
none
the script
go2NextTbl
Sub go2NextTbl() | |
Dim doc As Document | |
Dim selStart, selEnd, safeCount As Long | |
Application.ScreenUpdating = False | |
System.Cursor = wdCursorWait | |
Set doc = ActiveDocument | |
safeCount = 0 | |
If doc.Tables.Count = 0 Then | |
MsgBox "there's NO table in this doc." | |
Else | |
With Selection | |
If selStart >= doc.Tables(doc.Tables.Count).Range.Start Then | |
MsgBox "there's no table after this one." | |
doc.Range(selStart, selEnd).Select | |
Exit Sub | |
Else | |
If .Information(wdWithInTable) = True Then | |
selTbl | |
.MoveRight wdCharacter, 1 | |
End If | |
Do While safeCount < 5000 ' customize here if needed. | |
safeCount = safeCount + 1 | |
If .Information(wdWithInTable) = False Then | |
.MoveDown wdParagraph, 1 | |
Else | |
selTbl | |
.MoveLeft wdCharacter, 1 | |
Exit Sub | |
End If | |
Loop | |
End If | |
End With | |
End If | |
System.Cursor = wdCursorNormal | |
Application.ScreenUpdating = True | |
End Sub | |
Sub selTbl() | |
Selection.Tables(1).Select | |
End Sub |
output
- if the starting table is the last in the doc, a pop-up window saying so.
- or else, the cursor will now be at the beginning of the next table.
note to self
- the macro goes to the immediately next table irrespective of where the cursor is, as in, inside or outside a table.