How to find and delete a Heading with subtext?

H

hjdietmann

My Word document consists of several headings with subtext. I want to
find all headings (with the built-in heading style "Heading 4") that
contain a specific keyword (e.g. "TEST") and delete all text under
this heading.
In the example below, I want to delete heading 1.1.1.1 and 1.1.1.3
including the subtext.
In outline view mode, would click on the "+" tag, which highlights the
heading and press the del key.

+ 1.1.1.1 -> TEST - Heading
. Text line 1
. Text line 2
. more text

+ 1.1.1.2 -> Next Heading
. Text line 3

+ 1.1.1.3 -> TEST - Next Heading
. Text line 4

How can this be done by a VBA macro?
Thanks for any help.
Hans
 
H

Helmut Weber

Hi,

try this one:

Sub Test65523()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Style = "Heading 3"
While .Execute
If InStr(rDcm.Text, "TEST") > 0 Then
rDcm.Select
Selection.Bookmarks("\headinglevel").Range.Delete
End If
Wend
End With
End Sub

which works here and now in case,
there is no further heading that heading 3,
means no heading 4.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

hjdietmann

Hi,

try this one:

Sub Test65523()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Style = "Heading 3"
While .Execute
If InStr(rDcm.Text, "TEST") > 0 Then
rDcm.Select
Selection.Bookmarks("\headinglevel").Range.Delete
End If
Wend
End With
End Sub

which works here and now in case,
there is no further heading that heading 3,
means no heading 4.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP


Thank you very much, Helmut. The code works as expected.
Greetings from Stuttgart, Germany
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top