Thread: [Solved] Delete a Line in a TOC
View Single Post
 
Old 11-25-2024, 07:16 PM
KoolBreeze KoolBreeze is offline Windows 11 Office 2021
Novice
 
Join Date: Nov 2024
Posts: 8
KoolBreeze is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
Paul, I think you are not understanding what the OP is trying to do. My interpretation of what they are saying could be addressed by this macro.

This refreshes the TOC and then steps backwards through the entries and removes any TOC1s that are followed immediately by another TOC1 (doesn't have a child)

Code:
Sub TOC_Fiddler()
  Dim aPar As Paragraph, aTOC As TableOfContents, i As Integer, aRng As Range
  Set aTOC = ActiveDocument.TablesOfContents(1)
  aTOC.Update
  Set aRng = aTOC.Range
  For i = aRng.Paragraphs.Count - 1 To 1 Step -1
    If aRng.Paragraphs(i).Style = "TOC 1" And aRng.Paragraphs(i).Next.Style = "TOC 1" Then aRng.Paragraphs(i).Range.Delete
  Next i
End Sub



Thank you! With a slight mod to work on each of the TOCs, that works exactly as I want except I get a "Cannot edit Range." error if it attempts to delete the top entry in the TOC.


I was getting that same error before using similar code that used paragraph.range.delete but I didn't realize it was the just on the top entry that caused the error until I ran your code which steps backwards through the TOC.
Reply With Quote