Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-21-2024, 03:38 AM
RRB's Avatar
RRB RRB is offline Macro to navigate to next CR from present location Windows 11 Macro to navigate to next CR from present location Office 2021
Susan Flamingo
Macro to navigate to next CR from present location
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 263
RRB is on a distinguished road
Default Macro to navigate to next CR from present location

I have Googled and found nothing. ChatGPT offered me this:


Sub JumpToNextCrossReference()
Dim rng As Range
Dim fld As Field

' Define the range to search for cross-references
Set rng = Selection.Range



' Loop through each field in the document
For Each fld In ActiveDocument.Fields
If fld.Type = wdFieldRef Then
' Check if the field is within the defined range
If fld.Result.InRange(rng) Then
' Select the field and exit the loop
fld.Select
Exit For
End If
End If
Next fld
End Sub

But it is not working and I cannot understand why.

SF
Reply With Quote
  #2  
Old 03-21-2024, 04:59 AM
gmaxey gmaxey is offline Macro to navigate to next CR from present location Windows 10 Macro to navigate to next CR from present location Office 2019
Expert
 
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

It is not working because there are no cross reference fields in the defined range.
Your code would only find and select the first wdFieldRef in the selected text.


If you are on page 999 of a 1000 page document containing 25,000 cross references, does it make sense to loop through 24,900 or so to find the next one? If you want to find the next wdFieldRef from your current location then you need to define the range to be from where you are at to where you want the search to end. E.g., end of section, end of document whatever.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 03-21-2024, 05:03 AM
RRB's Avatar
RRB RRB is offline Macro to navigate to next CR from present location Windows 11 Macro to navigate to next CR from present location Office 2021
Susan Flamingo
Macro to navigate to next CR from present location
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 263
RRB is on a distinguished road
Default

Thank you for your attention!

So if we just want to move from one CR to the next we could theoretically define the range as just the current page or section and then move on to the next one from there? Per your expert opinion, how would advise to progress?

I though this would be so easy....
Reply With Quote
  #4  
Old 03-21-2024, 05:17 AM
RRB's Avatar
RRB RRB is offline Macro to navigate to next CR from present location Windows 11 Macro to navigate to next CR from present location Office 2021
Susan Flamingo
Macro to navigate to next CR from present location
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 263
RRB is on a distinguished road
Default

It occurred to me that the best way to go about this is to show field codes and then simply do a search on "ref" and tweak it from there...
Reply With Quote
  #5  
Old 03-21-2024, 05:51 AM
gmaxey gmaxey is offline Macro to navigate to next CR from present location Windows 10 Macro to navigate to next CR from present location Office 2019
Expert
 
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

I have already given you my opinion. In my opinion, the best way to go about this is to define a range (oRng) that starts at the end of the current selection and ends at the end of the document. Loop through the fields in that range and select the first REF field where its Result.Start is > oRng.Start.
__________________
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
VBA Command Buttons to Navigate thru Word SnakeDoctor Word VBA 2 07-06-2023 07:47 AM
Macro to navigate to specific point in line (Word 97) Genericname1111 Word 13 10-06-2019 08:54 PM
Can't navigate to entries in table of contents treez Word 4 12-25-2018 08:37 AM
Macro to navigate to next CR from present location Macro: How to get this macro to save to a specific location LOUF Word VBA 1 12-07-2015 06:47 PM
Macro Location eliz.bell Word 0 10-05-2009 07:44 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:44 AM.


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