Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 12-17-2025, 08:24 AM
TheBigBoss TheBigBoss is offline Delete a table of contents Windows 10 Delete a table of contents Office 2016
Advanced Beginner
Delete a table of contents
 
Join Date: Dec 2016
Posts: 58
TheBigBoss is on a distinguished road
Default

Hi,

First, fields can be locked, so unlocking is required.

Secondly, TOC is composed of FieldTOC that is attached to the last paragraph mark (a bit like the end paragraph of footnote) so deleting the TOC will never delete the TOC container unless this specific paragraph containing the fields is deleted.

Last, I had further issues if document has multiple TOCs.

So I had a brainstorming session for the dreaded AI and proposal was to:
a) Build a Collection of paragrah(1) (this is the end paragraph)
b) Then delete the whole fieldTOC (this will take care of TOC heading and fields - so no need of dealing with range)
c) And delete that paragraph(1) in reverse

And it works!!
PS. I wonder what is adding in newcollection under "parasToDelete.Add f.Code.Paragraphs(1)", is it the paragrah number?

Code:
Private Sub DeleteAllTOCs()
    Dim toc As TableOfContents, para As Paragraph, _
        i As Long, f As Field, parasToDelete As New Collection
    Application.ScreenUpdating = False
    Application.StatusBar = "We are deleting tables of content... Be patient..."
    For Each f In ActiveDocument.Fields
        If f.Type = wdFieldTOC Then
            f.Locked = False
            parasToDelete.Add f.Code.Paragraphs(1)
        End If
    Next f
    For Each f In ActiveDocument.Fields
        If f.Type = wdFieldTOC Then
            f.Delete
        End If
    Next f
    For i = parasToDelete.count To 1 Step -1
        parasToDelete(i).Range.Delete
    Next i
 Application.ScreenUpdating = True
    Application.StatusBar = ""
    Application.ScreenRefresh
    MsgBox "Done."
    Set toc = Nothing
End Sub

Last edited by TheBigBoss; 12-17-2025 at 08:25 AM. Reason: Removed my question asking how to mark it as SOLVED
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:33 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