kidman01
kidman01

Reputation: 945

Word VBA: How to get the current section number

I'm building a macro that loops through each word of a document and checks via a regex whether it matches a pattern and if so, writes the found word to an excel sheet. It goes like this:

For Each sentence In ActiveDocument.StoryRanges
    For Each w In sentence.Words
        myWord = w
        If TestRegExp(myPattern, myWord) Then
            WKS.Cells(myCount, 1).Value = myWord
            myCount = myCount + 1
        End If
    Next
Next

This part works fine. Now I would also like to get the section per found word (aka "in what section did the found word appear"). I found the command "selection.Information" but no matter what I try, I only get "Section = 1". Even if I just check the whole document for sections ("ActiveDocument.Sections.Count") I only get 1. So there must be something off with the sections, but this document definitely has sections. Has anybody an idea what I do wrong?

Upvotes: 2

Views: 7091

Answers (2)

Slai
Slai

Reputation: 22876

Sections are different from StoryRanges, and are part of the StoryRanges(wdMainTextStory) range. You can use For loop instead of For Each loop to get the WdStoryType number:

For i = 1 To ActiveDocument.StoryRanges.Count            ' or 1 To 17
    For Each w In ActiveDocument.StoryRanges(i).Words
        If TestRegExp(myPattern, w) Then
            WKS.Cells(myCount, 1).Value = myWord
            myCount = myCount + 1
            Debug.Print i, myCount, w                    ' i has the WdStoryType number
        End If
    Next
Next

Also, RegExp is probably not needed, as Word has wildcard Find and Replace https://superuser.com/questions/86397/wildcards-in-word, and VBA has the Like Operator

Upvotes: 0

William Doane
William Doane

Reputation: 1476

Page and section numbers in Word are difficult since the document's logical structure may not match the displayed text. I can, for example, reset page numbering in the middle of a document.

Similarly, a "section" to word is the separation of parts of the document by virtue of the insertion of a section break, whether next page, continuous, next odd, next even, etc. However, the reader often thinks of the "section" as the number that appears before a "heading 1" style paragraph. Again, I can reset those numbers mid-document. So, a document that has 3 sections (logical) might have only two headings: none in section 1, one in section 2 that says "1. Introduction", and another in section 3 that says "Appendix A. Glossary". The (logical) sections are still 1, 2, 3......

w = ActiveDocument.Sentences(10).Words(1) ' given some word in the document
MsgBox w.Information(wdActiveEndPageNumber) ' logical page number
MsgBox w.Information(wdActiveEndAdjustedPageNumber) ' displayed page number
MsgBox w.Information(wdActiveEndSectionNumber) ' logical section number

As for the displayed section number, which is there by virtue of being "Heading 1" style and that style having been set to a multi-level numbered format... getting the displayed number of that list item appears to be very difficult, in the general case.

Solutions I've seen involve searching for the previous paragraph that matches a heading style then getting the .ListFormat.ListString from that.

MsgBox w.GoTo(What:=wdGoToHeading, Which:=wdGoToPrevious).ListFormat.ListString

but that gets the previous heading of any level, not just "Heading 1".

Upvotes: 3

Related Questions