Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-21-2023, 12:18 PM
alex100 alex100 is offline Get the position of a string from the cursor position forward Windows 7 64bit Get the position of a string from the cursor position forward Office 2016
Advanced Beginner
Get the position of a string from the cursor position forward
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default Get the position of a string from the cursor position forward

In a Word document, how can I get the position of a string from the cursor position forward?



For example, if the cursor is on the 10th line from the start of the document and the string I am trying to find is on the 15th line, the function should return 5.

If the string is found in multiple places throughout the document (both before and after the cursor), the function should return the position of the first match after the cursor. If it is found only before the cursor (or not found at all), the function should return a null value.

Thank you!

Alex
Reply With Quote
  #2  
Old 02-21-2023, 04:02 PM
Guessed's Avatar
Guessed Guessed is offline Get the position of a string from the cursor position forward Windows 10 Get the position of a string from the cursor position forward Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,161
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

Word's vba doesn't normally do well with 'lines' because the wrapping of lines is dynamic (reliant on typeface, size, column width, text wrapping etc) and not structural. Paragraphs are the units that we can code with reliably. So let's assume you meant paragraphs.

Code:
Sub FindAhead()
  Dim aRng As Range, sFind As String, iResp As Integer, lngStart As Long, lngFound As Long
  sFind = "Save time in Word"
  Set aRng = Selection.Range
  lngStart = aRng.Start
  aRng.End = ActiveDocument.Range.End
  With aRng.Find
    .ClearFormatting
    .Text = sFind
    .Forward = True
    If .Execute Then
      lngFound = ActiveDocument.Range(lngStart, aRng.Paragraphs(1).Range.End).Paragraphs.Count - 1
      iResp = MsgBox("Do you want to jump to the next found instance?", vbYesNo, lngFound & " paragraphs ahead")
      If iResp = vbYes Then aRng.Select
    Else
      MsgBox "Ain't no more", vbCritical + vbOKOnly, "None found"
    End If
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 02-22-2023, 02:48 AM
alex100 alex100 is offline Get the position of a string from the cursor position forward Windows 7 64bit Get the position of a string from the cursor position forward Office 2016
Advanced Beginner
Get the position of a string from the cursor position forward
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

Wonderful! Thank you, Andrew!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get the position of a string from the cursor position forward select the sentence at cursor position ilcaa72 Word VBA 3 04-28-2017 05:42 AM
changing cursor position anand Word VBA 2 06-19-2015 05:18 AM
Increase Sentence Font at cursor position ilcaa72 Word VBA 8 02-20-2014 08:40 AM
Displaying cursor position within cell MiamiTom Excel 0 11-18-2011 09:02 AM
Retrieve position of Cursor - x & y positions SDondeti PowerPoint 0 05-19-2011 11:39 AM

Other Forums: Access Forums

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


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