Capture MS Word Sentence In A Variable

WonHop
WonHop used Ask the Experts™
on
Hello all.

I have code to search a MS Word Document.  That code works fine.  It finds all "TBD" and I store them and other data in a MS Access Table.

What I need:
When the "TBD" is found, I need code to select and capture the entire sentence in a variable.   See example sentence below.
I am doing this from MS Access.

TABLE 4.5.1 SHOWS THAT CAPTAIN AMERICA CAN LIFT THOR'S HAMMER AND IT IS TBD IF ANYONE ELSE CAN.

strCaputuredSentece = "TABLE 4.5.1 SHOWS THAT CAPTAIN AMERICA CAN LIFT THOR'S HAMMER AND IT IS TBD IF ANYONE ELSE CAN.."

Thanks
WonHop
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jeffrey CoachmanMIS Liason
Most Valuable Expert 2012

Commented:
Here is code I found a while ago to select a sentence in Word.
(Selects the sentence you cursor is already inside of) :
    Selection.Collapse
    Selection.Expand Unit:=WdUnits.wdSentence
 

Open in new window

See if you can adapt into your existing code...
Something like this perhaps?:
   
    'Your code to find/select "TBD" ends here.
    Selection.Collapse
    Selection.Expand Unit:=WdUnits.wdSentence

Open in new window


keep us posted
Retired
Top Expert 2012
Commented:
It would be nice to know how precisely how your code works and what it delivers.
If it creates the range of the "TBD" text as rngTBD, the sentence text can be found thus
strSentence = rngTDB.Sentences(1).Range.Text

Open in new window

John TsioumprisSoftware & Systems Engineer

Commented:
Check if this code works for you
For Each p In objDoc.Paragraphs
        Debug.Print p.Range.Text
        Set s = p.Range.Sentences(1)

Open in new window

If you could post some word sample along with the code we could provide some better help.

Author

Commented:
Here is a piece of the code I am using.  I hope this is enough to help.

Set FindRange = ActiveDocument.Range
FindRange.Find.Text = "TBD"

Do While FindRange.Find.Execute() = True
    pdPage = FindRange.Information(wdActiveEndPageNumber)
    Selection.Goto wdGoToPage, wdGoToAbsolute, pdPage
    pageNumberText = FindRange.Sections(1).Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Text
    pageNumberTrim = Trim(pageNumberText)
    intpageNumberLen = Len(pageNumberTrim)
Loop

Once the "TBD" is found, then I need to capture the sentence in a variable.

Thanks
WonHop

Author

Commented:
I got it:
=====================================
strSentence = FindRange.Sentences(1).Text
=====================================
GrahamSkan, I was getting and error message when I used your code.  It was telling me there was something wrong with the word "Range".  I removed it and it worked.  

Set FindRange = ActiveDocument.Range
FindRange.Find.Text = "TBD"

Do While FindRange.Find.Execute() = True
    pdPage = FindRange.Information(wdActiveEndPageNumber)
    Selection.Goto wdGoToPage, wdGoToAbsolute, pdPage
    pageNumberText = FindRange.Sections(1).Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Text
    pageNumberTrim = Trim(pageNumberText)
    intpageNumberLen = Len(pageNumberTrim)

    strSentence = FindRange.Sentences(1).Text
Loop

Thanks to all who responded.

WonHop

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial