Consulting

Results 1 to 2 of 2

Thread: Manage Word's Sections & Contents using TreeView

  1. #1

    Question Manage Word's Sections & Contents using TreeView

    Hello Experts


    • I have created a word template (Word64bit) and designed a userForm called “UpdateForm”.
    • I don’t have regular Module in my VBA code as all my codes are within the form itself.
    • Within the form, I have the following:
      • A button called "ScanSections"
      • A button called "DeleteSections"
      • A TreeView called "SectionTree".

    • When I press “ScanSections” button it scanned the entire document to retrieve sections names.
    • Specifically it looks for para.style = "Heading 1,PolHead1" Or para.style = "Heading 2,PolHead2" Or para.style = "PolHead3".
    • Represents matching results in “SectionTree” intended with respect to Hierarchy of each style
      • L1 under nodeL1
      • L2under nodeL2
      • L3 under nodeL3

    • All Nodes in the SectionTree are expanded and checkboxes are un-selected by default.


    test.jpg


    • Here is my code for “ScanSections” button for you to consider:


    Private Sub ScanSections_Click()
        Dim doc As Document
        Dim rng As Range
        Dim para As Paragraph
        Dim nodeL1 As node
        Dim nodeL2 As node
        Dim nodeL3 As node
       
        ' Get a reference to the active document
        Set doc = ActiveDocument
       
        ' Clear the existing nodes in the TreeView
        SectionTree.nodes.Clear
       
        ' Set the range to the entire document
        Set rng = doc.Content
       
        ' Loop through each paragraph in the document
        For Each para In rng.Paragraphs
            ' Check if the paragraph style matches any of the desired header styles
            If para.style = "Heading 1,PolHead1" Or para.style = "Heading 2,PolHead2" Or para.style = "PolHead3" Then
                ' Determine the level of the heading
                Select Case para.style
                    Case "Heading 1,PolHead1"
                        ' Add Level 1 heading to the TreeView
                        Set nodeL1 = SectionTree.nodes.Add(, , , para.Range.Text)
                        ' Disable the checkbox for Level 1 heading
                        SectionTree.nodes(nodeL1.Index).Checked = False
                        ' Expand the Level 1 node
                        SectionTree.nodes(nodeL1.Index).Expanded = True
                    Case "Heading 2,PolHead2"
                        ' Add Level 2 heading under the parent Level 1 heading
                        If Not nodeL1 Is Nothing Then
                            Set nodeL2 = SectionTree.nodes.Add(nodeL1.Index, tvwChild, , para.Range.Text)
                            ' Enable the checkbox for Level 2 heading
                            SectionTree.nodes(nodeL2.Index).Checked = False
                            ' Expand the Level 2 node
                            SectionTree.nodes(nodeL2.Index).Expanded = True
                        End If
                    Case "PolHead3"
                        ' Add Level 3 heading under the parent Level 2 heading
                        If Not nodeL2 Is Nothing Then
                            Set nodeL3 = SectionTree.nodes.Add(nodeL2.Index, tvwChild, , para.Range.Text)
                            ' Enable the checkbox for Level 3 heading
                            SectionTree.nodes(nodeL3.Index).Checked = False
                            ' Expand the Level 3 node
                            SectionTree.nodes(nodeL3.Index).Expanded = True
                        End If
                End Select
            End If
        Next para
    End Sub
    -------------------------------------------------------------------------------------------------
    I need your assistance to give me the code for "DeleteSections" button, to do the following:

      1. For each checked node within the " SectionTree", I want it to delete that header and its content from the document.
      2. Give me a message displaying the name(s) of the deleted section successfully.
      3. Refresh the “SectionTree”


    Thanks a million in advanced
    Regards
    Khaled
    Last edited by Aussiebear; 06-08-2023 at 12:39 AM. Reason: Added code tags to supplied code

  2. #2
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,997
    Location
    Welcome to VBAX Khaled. Please be patient as a Word Guru should be along soon.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Tags for this Thread

Posting Permissions

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