need code to get entire paragraph containing selected text

E

EdStevens

Working on an Excel macro that needs to get some info out of a Word doc.
Need a bit of code to open the doc, search for an occurrence of a text string
(will only occur once) then assign the entire record (paragraph) containing
that string to a string variable. The name of the file to be opened is
already known.

Thanks for any help.
 
F

fumei via OfficeKB.com

1. make an instance of Word - do you know how to do this?

2. open the Word document (you say you know the filename) - do you know how
to do this?

3. In your code have a WORD range object - note, make SURE you declare it as
a Word.Range: Dim r As Word.Range

If you declare it as just Range, as you are running it from Excel, it will
assume you mean an Excel Range.

Say your string variable is: Dim strThatParagraph As String

Set r = ActiveDocument.Range
With r.Find
.Text = "the search string"
.Execute
r.Expand Unit:=wdParagraph
strThatParagraph = r.Text
End With


strParagraph will be the paragraph that contains the Find.Text search string
 
E

EdStevens

fumei via OfficeKB.com said:
1. make an instance of Word - do you know how to do this?
Yes


2. open the Word document (you say you know the filename) - do you know how
to do this?
Yes


3. In your code have a WORD range object - note, make SURE you declare it as
a Word.Range: Dim r As Word.Range

If you declare it as just Range, as you are running it from Excel, it will
assume you mean an Excel Range.

Say your string variable is: Dim strThatParagraph As String

Set r = ActiveDocument.Range
With r.Find
.Text = "the search string"
.Execute
r.Expand Unit:=wdParagraph
strThatParagraph = r.Text
End With


strParagraph will be the paragraph that contains the Find.Text search string
That's exactly what I needed. Thank you for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top