#1
|
|||
|
|||
Method A or Method B
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. |
#2
|
||||
|
||||
I think that looping through all fields is inefficient and would prefer a more direct Find method. This doesn't move the selection if there are no more Ref fields so you could add a complication for that wrinkle (and potentially allow the find to wrap).
Code:
Sub FindMeSeymour() ActiveWindow.View.ShowFieldCodes = True With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = "^d REF" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .MatchCase = False .Execute End With ActiveWindow.View.ShowFieldCodes = False End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Andrew,
Clearly Method C. Thank you. I had forgotten all about the ^d switch for finding fields. I've taken this a bit further to permit jumping forward or back to the next field of type. A bit more code but wanted some escapes and options for looping. Code:
Sub FieldJumper() JumpToTypeField "REF", False End Sub Sub JumpToTypeField(strType As String, Optional bNext As Boolean = True) 'Method C Dim oRng As Range, oRngRef As Range Dim bShowCodes As Boolean Dim lngFind As Long, lngCount As Long Dim strMsg As String lngCount = 0 lngFind = 0 Set oRng = ActiveDocument.Range bShowCodes = ActiveWindow.View.ShowFieldCodes ActiveWindow.View.ShowFieldCodes = True With oRng.Find .ClearFormatting .Text = "^d " & strType .Forward = bNext .Wrap = wdFindStop .MatchCase = False Do While .Execute lngCount = lngCount + 1 If lngCount > 1 Then Exit Do Loop End With Wrap: Set oRng = Selection.Range.Duplicate Select Case lngCount Case 0 MsgBox "There are no " & strType & " fields in this document.", vbInformation + vbOKOnly, "NOT FOUND" Case Else With oRng.Find .ClearFormatting .Text = "^d " & strType .Forward = bNext .Wrap = lngFind .MatchCase = False If .Execute Then oRng.Select Else If lngCount > 1 Or Selection.Fields.Count = 0 Then ActiveWindow.View.ShowFieldCodes = False strMsg = "There are no subsequent " & strType & " fields in the document. Do you want to loop to the beginning of the document?" If Not bNext Then strMsg = "There are no preceding " & strType & " fields in the document. Do you want to loop to the end of the document?" If MsgBox(strMsg, vbQuestion + vbYesNo, "LOOP") = vbYes Then lngFind = 1 ActiveWindow.View.ShowFieldCodes = True GoTo Wrap End If Else ActiveWindow.View.ShowFieldCodes = False MsgBox "There are no preceding or subsequent " & strType & " fields in this document.", vbInformation + vbOKOnly, "SINGULAR FIELD" End If End If End With End Select lbl_Exit: ActiveWindow.View.ShowFieldCodes = bShowCodes Exit Sub End Sub |
#4
|
|||
|
|||
Regarding your question about moving to the next REF field, I agree that there are several aspects to consider. Additionally, taking into account different scenarios, such as missing REF fields in the document or having a highlight in the last REF field, is important. Using the BuiltIn Selection GoTo method as you suggested can be an effective solution, as it typically provides a more flexible and powerful way to control cursor movement in a document. This can also provide greater control over situations where a REF field is missing or when the cursor is in the last REF field
|
#5
|
|||
|
|||
What specific point are you trying to make that improves upon the last post in this thread. It seems that every consideration you mention has already been addressed. Would you care to elaborate on why, as you state, the Selection GoTo method provides more flexibility or power? The code posted is already controlling situations were a REF field is missing and when the cursor is in the last REF field. How can your suggestion (not your code because you didn't post any) provide greater control?
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Changing Recorded Macro Selection/Clipboard Method to Range/FormattedText Method | 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 |
Sorting method | Steve Bartko | Excel | 6 | 10-26-2016 11:38 AM |
Why no 'SaveCopyAs' method? | Cosmo | Word VBA | 4 | 10-18-2013 05:57 AM |