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.

Re: need code to get entire paragraph containing selected text by fumei

fumei
Wed Apr 09 12:28:22 PDT 2008

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


EdStevens wrote:
>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.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200804/1


Re: need code to get entire paragraph containing selected text by EdStevens

EdStevens
Thu Apr 10 06:51:07 PDT 2008



"fumei via OfficeKB.com" wrote:

> 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.

> EdStevens wrote:
> >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.
>
> --
> Message posted via OfficeKB.com
> http://www.officekb.com/Uwe/Forums.aspx/word-programming/200804/1
>
>