I am a VBA novice and I am trying to create an array from a 400 page word document in VBA made of chapter content. Each element of the array should contain all of the paragraphs following a bolded chapter title until the next chapter title. It may be better phrased as the information between chapter titles.
The chapter title is a sentence which is always bolded (and the only portion of the document which is bolded). The information following the chapter description may have multiple paragraphs and bulleted information but also may be completely empty in a few cases. In a case with empty chapter content, I would like a blank entry of some kind stored.
I've managed to make an array that has every paragraph as an array element. However, since there are sometimes multiple paragraphs and bulleted sections per chapter the amount of elements in the array is greater than the amount of chapters. The array also stores the chapter titles as their own element (I figured out how to remove titles from the array with like comparisons though). I am a bit lost after researching this subject for several hours today.
What would be a method to store all of the information between 'bolded chapter headings' as an element in an array?
Many thanks for your help!
Sub addUnderlinedWordsToArray()
On Error GoTo errhand:
Dim myWords() As String
Dim i As Long
Dim myDoc As Document: Set myDoc = ActiveDocument ' Change as needed
Dim aRange As Range: Set aRange = myDoc.Content
Dim sRanges As StoryRanges: Set sRanges = myDoc.StoryRanges
Dim ArrayCounter As Long: ArrayCounter = 0 ' counter for items added to the array
Dim Sentence As Range
Dim Paragraph As Range
Dim w As Variant
Dim myDescs() As String
Dim x As Variant
Application.ScreenUpdating = False
ReDim myWords(aRange.Words.Count) ' set a array as large as the
' number of words in the doc
For Each Paragraph In ActiveDocument.StoryRanges
For Each w In ActiveDocument.Paragraphs
myWords(ArrayCounter) = w
ArrayCounter = ArrayCounter + 1
Next
Next
On Error GoTo 0
Set myDoc = Nothing
Set aRange = Nothing
Set sRanges = Nothing
Set Ex0 = New Excel.Application
Set Wb0 = Ex0.Workbooks.Add
Ex0.Visible = True
Wb0.Sheets(1).Range("A1").Resize(UBound(myWords) + 1, 1) = WorksheetFunction.Transpose(myWords)
Application.ScreenUpdating = True
Debug.Print UBound(myWords())
Exit Sub
errhand:
Application.ScreenUpdating = True
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Subroutine Name: addUnderlinedWordsToArray" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
End Sub