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:
- For each checked node within the " SectionTree", I want it to delete that header and its content from the document.
- Give me a message displaying the name(s) of the deleted section successfully.
- Refresh the “SectionTree”
Thanks a million in advanced
Regards
Khaled