![]() |
|
#2
|
|||
|
|||
|
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 |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Word Table of Contents - when I title a heading "table", table of contents shows "table2"
|
jncuk | Word | 8 | 04-15-2020 07:01 PM |
Manipulating Headings so Image contents is correctly referenced in Table of Contents
|
dynamictiger | Word | 2 | 05-29-2019 05:41 AM |
Table of Contents -"Permanently" Setting Table of Contents Option
|
KRoger | Word | 3 | 03-01-2019 06:36 AM |
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 |