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
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Macropod

Retired Moderator
Joined
Aug 27, 2007
Messages
3,490
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

Board Regular
Joined
Apr 19, 2008
Messages
235
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
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.
 

Sarahmueller

New Member
Joined
May 17, 2020
Messages
24
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi Kennypete,

thank you very much!

Your code solved my problem!:)

Best regards,

Sarah
 

Watch MrExcel Video

Forum statistics

Threads
1,118,121
Messages
5,570,304
Members
412,318
Latest member
angoeyuan
Top