![]() |
#1
|
|||
|
|||
![]()
A few days ago there was a post in this forum regarding jumping from the current selection to the next REF field. Not wanting to give away another fish, I suggested the OP revise their existing code to set the starting range at the end of the current selection range.
A simple test proved that would achieve the basic goal. However, after giving it more thought, I realized that a process to jump to a specific field type should have a few escapes and consider A) The absence of any REF fields in a document and B) what to do (if anything) when the selection is already at last REF field in the document. Then I thought maybe using the BuiltIn Selection GoTo method might work as well or better. Here are the two variations I came up with: Code:
Sub Find_JumpToNextCrossReference() 'Method A Dim oRng As Range Dim oFld As Field Set oRng = Selection.Range.Duplicate oRng.End = ActiveDocument.Range.End If ActiveDocument.Fields.Count > 0 Then LoopToStart: If oRng.Fields.Count > 0 Then If oRng.Fields.Count = 1 And oRng.Fields(1).Result.Start = oRng.Start Then oRng.Start = oRng.Fields(1).Result.End + 1 If MsgBox("This is the last REF field. Do you want to loop to the beginning of the document?,", vbQuestion + vbYesNo, "LOOP") = vbYes Then oRng.Start = ActiveDocument.Range.Start End If End If For Each oFld In oRng.Fields If oFld.Type = wdFieldRef Then If oFld.Result.Start > oRng.Start Then oFld.Result.Select Exit For End If End If Next oFld If oRng.Start = Selection.Range.Start Then MsgBox "There are no REF fields in the document.", vbInformation + vbOKOnly, "NO CROSS REFERENCES FOUND" End If Else If MsgBox("There is no subsequent REF field in this document. Do you want to loop to the beginning of the document?", vbQuestion + vbYesNo, "LOOP FROM START") = vbYes Then oRng.Start = ActiveDocument.Range.Start GoTo LoopToStart End If End If Else MsgBox "There are no REF fields in this document.", vbInformation + vbOKOnly, "NO CROSS REFERENCES FOUND" End If lbl_Exit: Exit Sub End Sub Sub GoToNextREFField() 'Method B Dim lngStart As Long Dim lngCount As Long Dim lngFldCount As Long Dim oRngRef As Range, oRng As Range Application.ScreenUpdating = False Set oRngRef = Selection.Range.Duplicate lngFldCount = ActiveDocument.Fields.Count LoopToStart: lngCount = 0 Do Set oRng = Selection.Range.Duplicate lngStart = Selection.Range.Start Selection.GoToNext (wdGoToField) 'Get field in selection Selection.MoveEnd , 1 lngCount = lngCount + 1 If Selection.Start = oRng.Start Then Exit Do Loop Until Selection.Fields(1).Type = wdFieldRef Select Case True Case lngCount > lngFldCount And Selection.Fields.Count = 0 MsgBox "There are no REF fields in the document.", vbInformation + vbOKOnly, "NOTHING TO GOTO" Case lngCount > lngFldCount And Selection.Fields(1).Type <> wdFieldRef MsgBox "There are no REF fields in the document.", vbInformation + vbOKOnly, "NOTHING TO GOTO" oRngRef.Select Case Selection.Fields(1).Type <> wdFieldRef Or (Selection.Start = oRngRef.Start And Selection.Fields(1).Type = wdFieldRef) If MsgBox("There is no subsequent REF field in this document." & vbCr + vbCr _ & "Do you want to loop to the start of the document?", vbInformation + vbYesNo, "LOOP") = vbYes Then ActiveDocument.Range(0, 0).Select GoTo LoopToStart Else oRngRef.Select End If End Select Application.ScreenUpdating = True lbl_Exit: Exit Sub End Sub Interested in any critical, constructive comments on either method. |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
zanodor | Word VBA | 5 | 07-15-2022 01:25 AM |
VBA Excel method Not working | grandneos | Excel | 2 | 01-07-2022 11:19 AM |
OrganizerCopy method for ActiveDocument | AnnaNmty | Word VBA | 4 | 07-25-2018 08:31 PM |
![]() |
Steve Bartko | Excel | 6 | 10-26-2016 11:38 AM |
![]() |
Cosmo | Word VBA | 4 | 10-18-2013 05:57 AM |