Consulting

Results 1 to 4 of 4

Thread: Struggling to get Heading 1 Ranges

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    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

Posting Permissions

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