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