# Delete content of Word chapters but keep the title - VBA



## Sarahmueller (May 19, 2020)

Hello,

I hope all of you are healthy.

I have a word document that contains several chapters including assessments concerning various topics.

Since I have to update the document each week, I have tried to write a macro that deletes the contents of all chapters, but keeps the title of the chapter and therefore the structure of the document.
I have tried to use the macro-recorder, but because the length of the assessment varies each time, the macro does not work.

To solve my problem, I have inserted section breaks into my document that equal the text that should be deleted. The titles of the sections are excluded, so that they won´t be deleted.

The text of chapter 2.1 equals the 4th section as defined by my section breaks (continous).

I have then tried the following code (to delete the content of chapter 2.1 in my document = 4th section break)


```
Sub DeleteSectionnumber2point1()

Dim SECS As Word.Section
 
  Set SECS = Selection.Sections(4)
  
  SECS.Range.Select
  
  SECS.Range.Delete
 
End Sub
```


Sadly, the macro does not work and the error " Run time error 5941: The requested member of the collection does not exist" appears.


Do you have ideas how to solve my problem and modify my macro?

Best regards and thanks in advance,

Sarah


----------



## Macropod (May 19, 2020)

You need to provide more information. For example, do your chapter titles employ a particular Heading Style? It's difficult to see how, on your description, your chapter titles would not be in Word Sections.


----------



## kennypete (May 19, 2020)

Hi Sarah

If you want to run this from Excel rather than Word itself, then this code will strip all the tables plus all the paragraphs from the document _except those paragraphs that are styled "Heading"_:


```
Sub DeleteNonHeadingsWordDoc()
    Dim WordApp As Object, WordDoc As Object, para
    Set WordApp = CreateObject(Class:="Word.Application")
    WordApp.Visible = True
    Set WordDoc = WordApp.Documents.Open("D:\_Share\20200519\Strip_All_Bar_Headings.docx") ' change to your file, add a dialog or whatever
    With WordApp
    For Each tabl In WordDoc.Tables
        tabl.Delete
    Next
    For Each para In WordDoc.Paragraphs
        If Left(para.Style, 7) <> "Heading" Then ' Or you could modify it to the styles you want to retain
            para.Range.Delete
        End If
    Next
    End With
    WordApp.ActiveDocument.Save
    WordApp.ActiveDocument.Close
    WordApp.Quit
    Set WordApp = Nothing
    Set WordDoc = Nothing
End Sub
```

If your macro is run from Word (i.e. the Word application itself) then the VBA is simpler (this presuming that the macro is running on the active document):


```
Sub DeleteNonHeadings()
    For Each tabl In ActiveDocument.Tables
        tabl.Delete
    Next
    For Each para In ActiveDocument.Paragraphs
        If Left(para.Style, 7) <> "Heading" Then
            para.Range.Delete
        End If
    Next
End Sub
```

You could adjust either of these to strip whatever other objects you don't want to retain in your Word document between the headings.


----------



## Sarahmueller (May 19, 2020)

Hi Kennypete,

thank you very much!

Your code solved my problem!

Best regards,

Sarah


----------

