logo
search
Document Editing Problems

How to Remove a Word Table of Contents Using VBA Without SendKeys

Bushra ParveenBushra Parveen Sep 29, 2026 868 views

Question details

The user needs to delete a Table of Contents in Word using a VBA macro without relying on SendKeys, ensuring the surrounding container is completely removed.

How to Remove a Word Table of Contents Using VBA Without SendKeys
Product
Microsoft Word
Device & OS
not provided
Scenario
Writing a VBA macro to clean up or reformat a Word document by programmatically deleting its Table of Contents.
Observed behavior
Using the standard TableOfContents.Delete method removes the text but leaves the Content Control container behind, while using SendKeys to simulate ribbon shortcuts is unreliable.
Before you start

Ensure your document is saved as a Macro-enabled document (.docm) and that you have enabled the Developer tab in your Word settings to access the VBA editor.

Solution 1Recommended

Use the ExecuteMso Command in VBA

Invoking Word's built-in 'Remove Table of Contents' ribbon command via ExecuteMso guarantees a clean deletion of the TOC and its container box.

The Application.CommandBars.ExecuteMso method allows you to trigger built-in Ribbon commands directly from VBA. Using "TableOfContentsRemove" mimics the exact behavior of clicking the Remove Table of Contents button in the UI.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor in Word.

2
Insert a New Module

In the Project Explorer pane on the left, right-click your document project, select Insert, and then click Module.

3
Add the ExecuteMso Macro

Copy and paste the following code into the module window: Sub RemoveTOC() Application.CommandBars.ExecuteMso "TableOfContentsRemove" End Sub

4
Run the Macro

Place your cursor inside the Sub procedure and press F5, or click the green Run button, to instantly and cleanly remove the Table of Contents from your active document.

Use the ExecuteMso Command in VBA
Reliability Improved: Unlike the SendKeys method (Alt+S, T, R), ExecuteMso runs seamlessly in the background and will not fail if another application steals window focus.
Easily Manage TOCs in WPS

Remove a Table of Contents Easily in WPS Writer

If you want to skip writing VBA macros entirely, WPS Writer provides a simple, one-click solution to completely remove a Table of Contents and its container box without any coding.

  1. 1. Open your Document in WPS Writer: Launch WPS Office and open the document containing the Table of Contents you wish to remove.
  2. 2. Navigate to the References Tab: Click on the References tab located on the top navigation ribbon.
  3. 3. Open the TOC Menu: Click the Table of Contents dropdown button to reveal the available formatting and editing options.
  4. 4. Select Remove Table of Contents: Click on 'Remove Table of Contents' at the bottom of the dropdown list. The TOC and its container will be instantly deleted.
Perfectly compatible with Microsoft Word .docx formatsRemove a Table of Contents in just two clicks without using VBA codeLightweight alternative to MS Word with a highly familiar interfaceFree to download with comprehensive document editing features
microsoft office alternative - wps office

Frequently Asked Questions

Why does TableOfContents.Delete leave a box behind?

In modern versions of Word, the Table of Contents is wrapped inside a Content Control box. The standard VBA .Delete method removes the text fields inside but fails to target the outer Content Control, leaving an empty box on the page.

What is the problem with using SendKeys in VBA?

SendKeys simulates actual keyboard typing. If a user clicks their mouse, a notification pops up, or another program steals focus while the macro is running, the keystrokes are sent to the wrong window, causing the script to fail or behave unpredictably.

Can I use ExecuteMso to target a specific TOC if there are multiple?

No. The ExecuteMso command triggers the ribbon button, which applies to the active selection or the first TOC it detects in context. To precisely target specific TOCs, you would need to iterate through Document.TablesOfContents, though this brings back the Content Control deletion issue.

How can I update a TOC instead of deleting it using VBA?

If you want to refresh the page numbers and headings rather than remove them, use the following VBA line: ActiveDocument.TablesOfContents(1).Update. This refreshes the first TOC in your document.