setup
now that i've got a one-click solution of jump to the next table (see more in here, i'd like its reverse, i.e. jumping backward to the previous table.
solution
the plan
- check if the current table is the first in the doc.
- if yes, prompt. if no, go backward to right before the current table, and keep going to the previous 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
go2PrevTbl
| Sub go2PrevTbl() | |
| 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(1).Range.Start Then | |
| MsgBox "there's no table before this one." | |
| doc.Range(selStart, selEnd).Select | |
| Exit Sub | |
| Else | |
| If .Information(wdWithInTable) = True Then | |
| selTbl | |
| .MoveLeft wdCharacter, 2 | |
| End If | |
| Do While safeCount < 5000 ' customize here if needed. | |
| safeCount = safeCount + 1 | |
| If .Information(wdWithInTable) = False Then | |
| .MoveUp 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.