View Single Post
 
Old 02-21-2023, 04:02 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,163
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