How to Remove a Word Table of Contents Using VBA Without SendKeys
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.

- 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.
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.
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.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor in Word.
In the Project Explorer pane on the left, right-click your document project, select Insert, and then click Module.
Copy and paste the following code into the module window: Sub RemoveTOC() Application.CommandBars.ExecuteMso "TableOfContentsRemove" End Sub
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.

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. Open your Document in WPS Writer: Launch WPS Office and open the document containing the Table of Contents you wish to remove.
- 2. Navigate to the References Tab: Click on the References tab located on the top navigation ribbon.
- 3. Open the TOC Menu: Click the Table of Contents dropdown button to reveal the available formatting and editing options.
- 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.

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.




