Delete content of Word chapters but keep the title - VBA

Sarahmueller

New Member
Joined
May 17, 2020
Messages
24
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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)

VBA Code:
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
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
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.
 
Upvote 0
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":

VBA Code:
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):

VBA Code:
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.
 
Upvote 0
Hi Kennypete,

thank you very much!

Your code solved my problem!:)

Best regards,

Sarah
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,442
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top