PDA

View Full Version : [SOLVED:] Extract a Paragraph's Outline Reference



headley
05-18-2010, 11:54 AM
Sorry for long post, but I want to make sure I explain the problem properly.

In a document I need to find each sentence which contains a particular word and cross-reference it to the outline number it appears under. I can easily find the word and expand to get the sentence, but storing the corresponding outline reference is proving tricky. It’s OK if the search word appears in the header for each outlined section, but when it appears in the body text I can’t find a way to reference it. An example follows of what I’d like to do (please forgive shoddy references to outlines and sections – I hope the example makes it clear):

1 This is the header for section 1 (i.e. outlined)
This is a paragraph in section 1. This sentence contains the STRING I need to find. So I need to pull out the last sentence and then I need to tag it with it's relevant section number - in this case "1".

2 This is the header for section 2
Ditto above. This para is clearly part of document section 2 and I need to find this sentence (because it's got STRING in it) and tag it with "2"

2.1 This is the header for Outline 2.1
This para is clearly part of section 2.1. This sentence contains the STRING so I need to extract it and tag it with "2.1"

2.2 This is the header for section 2.2
I can easily find the sentences which contain STRING in these paragraphs, but I can't find any way to extract the section (outline) reference. I’ve tried using .listformat.liststring property, but this only works if the STRING is in the actual header text. If the STRING is in a normal paragraph below the header (like this one is) then the .listformat.liststring returns nothing

Thanks in advance for any help.

HG.

TonyJollans
05-19-2010, 06:13 AM
I don't know any easy way to get it, but what about this:


Sub TestIt()
MsgBox ParentLevel(YourRange.Paragraphs(1))
End Sub

Function ParentLevel(Para As Word.Paragraph) As String
Dim ParaAbove As Word.Paragraph
Set ParaAbove = Para
Do While ParaAbove.OutlineLevel = Para.OutlineLevel
Set ParaAbove = ParaAbove.Previous
Loop
ParentLevel = ParaAbove.Range.ListFormat.ListString
End Function

headley
05-19-2010, 11:09 AM
Thanks Tony - that works fine. Your help is appreciated.

HG

fumei
05-20-2010, 08:54 AM
This para is clearly part of section 2.1. It is not clear to me. There is no such thing as a Section 2.1 - at least as far as Word determines Section.

headley
05-20-2010, 10:56 AM
Gerry - hence my apology in the original post for shoddy terminology. I appreciate that technically "section" probably means something else in Word so that's why I submitted the long post to make sure that you guys would understand my intentions.

fumei
05-20-2010, 01:46 PM
And we appreciate that!

Tony's code should work, except for multiple paragraphs after the heading (not header).

2.2 yadda
This is a paragraph and Tony's code will get the .Previous paragraph level (2.2)

This is the NEXT paragraph and Tony's code will not get 2.2

2.3 whatever

Now you could, if it is really important - and I would have to be convinced - keep moving back and back until you DO hit a .Previous that does return an appropriate .ListValue - rather than ListString.

headley
05-20-2010, 02:25 PM
Thanks, Gerry. Appreciation appreciated.

Tony's code *does* find the text and relevant para/outline/section level for multiple paras beyond the heading. Maybe I implemented it wrong!

Regards,

HG.

fumei
05-20-2010, 02:30 PM
Darn darn darn.

Oh well, another instance of NOT fully checking myself, and arrogantly thinking I know what the heck I am talking about.

I believe that gives Tony a ticky point.





Don't listen to me. Listen to Tony.