Consulting

Results 1 to 4 of 4

Thread: Struggling to get Heading 1 Ranges

  1. #1
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location

    Struggling to get Heading 1 Ranges

    I have a document with multiple sections (doesn't matter) with Heading 1 paragraphs, and I'm trying to get each Heading 1 'chunk' into a range so that I can do statistics, etc. on just that piece

    Example document structure:

    DocStart
    text

    H1 #1
    text

    H1 #2
    text

    H1 #3
    text
    DocEnd

    So I'm trying to get a loop that returns 4 Ranges:

    DocStart to just before H1 #1 (first char at top to last char before the H1 paragraph)
    H1 #1 to just before H1 #2
    H1 #2 to just before H1 #3
    H1 #3 to DocEnd

    Can someone get me started please?

    Edit#2 - This is what I have so far. Seems to work, but not sure if it'll stand the test of time. Word's object model is pretty much a mystery to me, so I'm open to any suggestions

    Option Explicit
    
    
    Sub ChunkHeading1()
        Dim rChunk As Range
        Dim n As Long
        
        Selection.HomeKey Unit:=wdStory
            
        Set rChunk = Selection.Range
        
        With Selection.Find
            .ClearFormatting
            .Style = ActiveDocument.Styles("Heading 1")
    
    
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            
            Do While .Execute
                
                'remember end position = before the start of the Heading 1 paragraph
                Selection.MoveLeft Unit:=wdCharacter, Count:=2
            
                rChunk.End = Selection.End
                
                n = n + 1
                MsgBox "Chunk # " & n & " -- " & rChunk.ComputeStatistics(wdStatisticWords)
                
                rChunk.Move Unit:=wdParagraph, Count:=2
                rChunk.Select
                
            Loop
        
            'from the last Heading 1 to end of doc
            Selection.EndKey Unit:=wdStory, Extend:=wdExtend
            n = n + 1
            MsgBox "Chunk # " & n & " -- " & Selection.Range.ComputeStatistics(wdStatisticWords)
        
        End With
    
    
    End Sub
    Attached Files Attached Files
    Last edited by Paul_Hossler; 07-30-2020 at 12:55 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location

    @macropod -- small issue

    Quote Originally Posted by macropod View Post
    That's great

    Thanks

    Works well, but sometimes there's text before the first Heading 1

    Capture.JPG

    Is there a way to make it work to show the word count for such text also?

    My long range project is to take each such ranges (which only are used to display word counts right now) and do some other statistics and maybe some processing on the range

    Thanks
    Attached Files Attached Files
    Last edited by Paul_Hossler; 08-02-2020 at 04:42 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    There are numerous ways of addressing this. For example, have the macro test whether the first:
    • Heading1 match is at the start of the document and, if not, capture the preceding text; and
    • paragraph is in the Heading1 Style and, if not, temporarily insert one there before running the rest of the code.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •