Thread: [Solved] Delete a Line in a TOC
View Single Post
 
Old 11-26-2024, 02:11 AM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

The first paragraph is particularly hard to delete and I guess this is because the field code is held within it. To keep things simple, I would first create a style in the document that has your 'hidden' attributes. I would actually use the font hidden property but making it white and tiny is similar in result and doesn't matter if the user has their options set to view hidden text.

There is no need to step backwards through the TOCs - just the paragraphs within the TOCs.

Then this code could be as simple as...
Code:
Sub TOC_Fiddler()
  Dim aTOC As TableOfContents, i As Integer, aRng As Range
  'Assumes you have a style called 'TOC Hidden' to apply to lines you want to hide
  For Each aTOC In ActiveDocument.TablesOfContents
    aTOC.Update
    With aTOC.Range
      For i = .Paragraphs.Count - 1 To 1 Step -1
        If .Paragraphs(i).Style = "TOC 1" And .Paragraphs(i).Next.Style = "TOC 1" Then
          .Paragraphs(i).Style = "TOC Hidden"
        End If
      Next i
    End With
  Next
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote