Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Find Paragraph Heading Text

Status
Not open for further replies.

wadasczi

Programmer
May 31, 2013
2
US
Folks,
I cluged together from multiple sources the following code which finds all the "shalls" in a document and the associated paragraph numbers, opens an excel spredsheet and pastes the info into there...it works great. The problem is I want to get the paragraph number title too. I am a tad bit stumped at this stage and have spent way to long trying to figure it out. Thank you for helping!
Wally

Example; 3.1.2 Introduction Level 2
This object shall jumps over fences.

The output will be put into an excel spredsheet in cell A1: 3.1.2 ; This object shall jumps over fences ;
I can then perform a text to columns manipulation and get what I want. How do I get the "Introduction to Level 2" information?????

My Code:
Sub FindAllShalls()

Dim doc As Document, par As Paragraph, str As String, str1 As String
Dim xlApp As Excel.Application, wbk As Excel.Workbook
Dim sht As Excel.worksheet, rng As Excel.Range
Set doc = ActiveDocument
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set wbk = xlApp.Workbooks.Add
Set sht = wbk.Sheets(1)
Set rng = sht.Range("A1")

Dim strSection As String
Dim wdParagraph As Paragraph
Dim wdSentence As Range

strSection = "N/A"

For Each wdParagraph In ActiveDocument.Paragraphs

If wdParagraph.Range.ListFormat.ListString <> "" Then strSection = wdParagraph.Range.ListFormat.ListString

If InStr(1, wdParagraph.Range.Text, "shall", vbTextCompare) <> 0 Then 'if word found in paragraph

For Each wdSentence In wdParagraph.Range.Sentences 'search each sentence in paragraph until word is found
If InStr(1, wdSentence, "shall", vbTextCompare) <> 0 Then
str = strSection & " ; " & wdSentence & " ; "
rng.Value = str
Set rng = rng.Offset(1, 0)
End If
Next
End If

Next

End Sub
 
Probably should have said earlier that this question is better asked in forum707, since this looks to be VBA rather than VB

having said that, the following might help :

wdParagraph.range.Previous(1).Sentences(1)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top