Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-23-2024, 07:39 AM
gmaxey gmaxey is online now Method A or Method B Windows 10 Method A or Method B Office 2019
Expert
Method A or Method B
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default 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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #2  
Old 03-24-2024, 08:26 PM
Guessed's Avatar
Guessed Guessed is offline Method A or Method B Windows 10 Method A or Method B Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 03-25-2024, 02:39 AM
gmaxey gmaxey is online now Method A or Method B Windows 10 Method A or Method B Office 2019
Expert
Method A or Method B
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #4  
Old 03-31-2024, 01:34 PM
TessaMurillo TessaMurillo is offline Method A or Method B Windows Vista Method A or Method B Office 2010
Advanced Beginner
 
Join Date: Mar 2024
Posts: 33
TessaMurillo has a little shameless behaviour in the past
Default

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
Reply With Quote
  #5  
Old 03-31-2024, 01:50 PM
gmaxey gmaxey is online now Method A or Method B Windows 10 Method A or Method B Office 2019
Expert
Method A or Method B
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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?
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Method A or Method B 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
Method A or Method B Sorting method Steve Bartko Excel 6 10-26-2016 11:38 AM
Method A or Method B Why no 'SaveCopyAs' method? Cosmo Word VBA 4 10-18-2013 05:57 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:58 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft