0

I have a userform that contains nine checkboxes, each corresponding with an Outline Level 1 heading (style Heading11) within the document.

I want to be able to tick any number of these checkboxes and have the selected heading(s) & the text within that 'section'/up until the next Heading11, deleted from the document.

For example, if I tick CbxISR and CbxPPL the document will be searched for Heading11 text "Industrial Special Risk" and "Public Liability".

I tried to adapt the code found in THIS thread, which uses the .Find method, but it is specific to searching just one non-variable text value.

How can I work with checkboxes that are ticked?

Community
  • 1
  • 1
NicKitty
  • 35
  • 5

2 Answers2

0

You could loop through your checkboxes, passing part or all of each heading's text and the heading level # to a macro such as the following for each checked checkbox:

Sub DeleteHeadingSpanText(StrTxt As String, h As Long)
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = StrTxt
    .Style = "Heading " & h
    .Replacement.Text = ""
    .Format = True
    .Forward = True
    .Wrap = wdFindContinue
    .Execute
  End With
  If .Find.Found = True Then
    .Duplicate.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel").Delete
  End If
End With
End Sub

For example, suppose you have a Heading 1 whose text consists of:

Lorem ipsum dolor sit amet.

You can delete all the content associated with that heading via:

Sub Demo()
Call DeleteHeadingSpanText("ipsum dolor", 1)
End Sub

Note that only a partial string from the heading is required. Note also that Word's heading Styles only span 'Heading 1' through 'Heading 9'; there is no 'Heading11'.

macropod
  • 12,757
  • 2
  • 9
  • 21
  • Thanks @macropod for the above. I ended up just bookmarking each heading & content block and using a very basic ```IF CbxABC.value = true then bookmark(ABC).value = vbnullstring``` within my existing macro. Was much easier! – NicKitty May 06 '20 at 23:48
  • Do note that approach will fail if someone (accidentally) deletes the bookmark or inserts some new content between the bookmark and the next heading. – macropod May 06 '20 at 23:50
  • Thanks @macropod - I'm giving this another crack but am unsure on a few bits in your code above. Can you please explain where I define the ```strTxt```? Why am I "Replacing" format and text or using a .Duplicate? Also, what is ```h``` referring to? – NicKitty May 12 '20 at 05:18
  • As explained in my answer, the DeleteHeadingSpanText sub takes two arguments :1, StrTxt, which is all or part of the Heading whose text you want to delete (if a part, sufficient to distinguish it from any other heading at the same level); and 2. h, which is the heading Style's level # (e.g. the 1, 2, and so on, in Heading 1, Heading 2, etc). – macropod May 12 '20 at 05:55
0

I ended up using the below code, which works perfectly. Thought I would share it in case anyone else struggled with this!

Note that I CollapseAllHeadings within my userform_Initialise sub to make sure that this works properly.

Public Sub ComOK_Click()

'Warning msgbox confirm sections being deleted
Dim answer As Integer
Dim strResult As String
Dim strDelete As String
Dim obj As Object

strResult = "You are about to remove the following templates from this document:" & vbCr

For Each obj In Me.Controls
    Select Case TypeName(obj)
        Case "CheckBox"
            If obj.Value = True Then
                strResult = strResult & vbCr & obj.Caption
            End If
    End Select
Next obj

answer = MsgBox(strResult & vbCr & vbCr & "THIS CANNOT BE UNDONE after saving your document." 
& vbCr & "Do you want to continue?", vbOKCancel, "WARNING")

If answer = vbOK Then

UserFormSections.Hide
Application.ScreenUpdating = False

'Delete sections from document
For Each obj In Me.Controls
    Select Case TypeName(obj)
        Case "CheckBox"
            If obj.Value = True Then
                strDelete = obj.Caption
                    Call DeleteHeading(strDelete)
            End If
    End Select
Next obj
Else
End If

'Expand remaining headings
With ActiveDocument.ActiveWindow.View
    .ExpandAllHeadings
End With
'land back on titlepage after sub
ActiveDocument.Bookmarks("DeleteTemplate").Select

Application.ScreenUpdating = True

'Confirm delete to user
MsgBox "Your selected section(s) have been deleted.", vbOKOnly, "Deleted"
End Sub

Sub DeleteHeading(strText As String)
Dim HeadingF As Range
Set HeadingF = ActiveDocument.Content

'Search for match
    With HeadingF.Find
        .Style = "Heading 2"
        .Text = strText
        .Forward = True
        .Wrap = wdFindStop
        .MatchWholeWord = True
        .MatchCase = False
        .MatchSoundsLike = False
        .MatchWildcards = False
        .MatchAllWordForms = False
        .Execute
    End With
'Delete match
    If HeadingF.Find.Found Then
        HeadingF.Select
        Selection.MoveEnd wdParagraph
        Selection.Delete
    End If
End Sub```
NicKitty
  • 35
  • 5