Link to home
Start Free TrialLog in
Avatar of traciwho
traciwho

asked on

Get word text after search string

I'm stumped; I've played around and adapted the following from some code that I found here (thanks yall) and I can ALMOST get what I need, but not quite.

If you see the following text, I am trying to retrieve the paragraph that is after I have searched and found the common text "Purpose/scope:".  The code below selects all of it.  I'd like to copy the text either into field in another word doc, or into an access field, but for right now all I'm really concerned about is how to select and copy only paragraph following purpose/scope, not the entire section.

PURPOSE/SCOPE:      This procedure will provide the guidelines to assist the Technician in the shutdown of the Towers.

Here's my code:  (once again, "borrowed" from this site)

Sub searchtext()
    Dim rng As Range
    Dim para As Paragraph
    Dim sSearch$
   
   sSearch = "Purpose/Scope:"
   
    Set rng = ActiveDocument.Range
    With rng.Find
        .Execute Findtext:=sSearch
        Selection.MoveRight Unit:=wdWord, Count:=1
    End With
   
    Set para = rng.Paragraphs(1)
    Set rng = para.Range
       
    For Each para In rng.Paragraphs
      If Selection.Find.Execute(sSearch, Wrap:=wdWrapAlways) = True Then
        rng.Start = Selection.Range.Start
       rng.Expand Unit:=wdParagraph
   rng.Select
   rng.Copy
   Else
      MsgBox "Searchtext " & sSearch & " not found."
   End If
    Next para
End Sub

Thanks in advance!

Traci
ASKER CERTIFIED SOLUTION
Avatar of CitizenRon
CitizenRon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of traciwho
traciwho

ASKER

Thanks to you both!

CitizenRon - your code helped me to get rid of the Purpose/Scope: text

Graham Skan - yours helped me to get to the piece I needed to copy behind the text.
I need to tweak it a little more, then I'll post the finished result.  Thanks again for the quick responses!

Traci