Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #6  
Old Yesterday, 02:50 PM
gmaxey gmaxey is offline Delete a table of contents Windows 10 Delete a table of contents Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,638
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Charles,


Well, you are right. That code did nothing with your example.

I think part of the problem will be dealing with TOCs that are in a container as well as those that are not and not with a preceding TOC Heading paragraph.


Both of these seem to work with your examples:

Code:
Sub DeleteTOCs_and_Containers()
Dim oRng As Range
Dim TOC As TableOfContents
Dim lngIndex As Long
  For lngIndex = ActiveDocument.TablesOfContents.Count To 1 Step -1
    Set TOC = ActiveDocument.TablesOfContents(lngIndex)
    TOC.Range.Fields(1).Locked = False
    Set oRng = TOC.Range.Fields(1).result
    oRng.MoveStart Unit:=wdParagraph, Count:=-2
    oRng.MoveEnd Unit:=wdCharacter, Count:=1
    Select Case True
      Case oRng.Paragraphs(1).Style = "TOC Heading"
      Case Else
        oRng.MoveStart wdParagraph, 1
    End Select
    oRng.Delete
  Next lngIndex
lbl_Exit:
  Exit Sub
End Sub
Sub DeleteTOCs_and_ContainersII()
Dim TOC As TableOfContents
Dim oRng As Range
Dim lngIndex As Long
  For Each TOC In ActiveDocument.TablesOfContents
    Set oRng = TOC.Range.Paragraphs.First.Previous.Range
    Application.CommandBars.ExecuteMso ("TableOfContentsRemove")
    On Error GoTo Skip
    If oRng.Style = "TOC Heading" Then
      oRng.Delete
    End If
Skip:
  Next TOC
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete a table of contents Word Table of Contents - when I title a heading "table", table of contents shows "table2" jncuk Word 8 04-15-2020 07:01 PM
Delete a table of contents Manipulating Headings so Image contents is correctly referenced in Table of Contents dynamictiger Word 2 05-29-2019 05:41 AM
Delete a table of contents Table of Contents -"Permanently" Setting Table of Contents Option KRoger Word 3 03-01-2019 06:36 AM
Delete a table of contents Code to find a named (bookmarked?) table, replicate a row or table, and delete a specified table. kevinbradley57 Word VBA 9 09-21-2017 04:58 PM
Text inside text boxes create headings inside my table of contents!!! How do I delete the created he carstj Word 3 04-11-2016 12:46 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:32 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft