Creating and Updating table of contents in word document using C#


Create a new windows application project and add a button to it.

On click of that button, we will open a document and first add a heading 1 programmatically and then insert a table of content programmatically and update it

Than we will add reference to (Word 10.0 or 11.0 object library) within COM tab of Add reference dialog box.

After adding reference, we’ll add this directive using Microsoft.Office.Interop.Word than we’ll put the following code in the button click event handler

// For optional parameters create a missing object

object missing = System.Reflection.Missing.Value;

// Create an object for filename which is the file to be opened

object fileName = @”C:\MySecond.doc”;

// Create an object of application class

ApplicationClass WordApp = new ApplicationClass();

// open the document specified in the fileName variable

Document adoc = WordApp.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing,

ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,

ref missing, ref missing, ref missing);

Range myRange = adoc.Range(ref missing, ref missing);

myRange.InsertAfter(“Hello Mickey Mouse GG “);

object oStyleName = “Heading 1”;

myRange.set_Style(ref oStyleName);

object start=WordApp.ActiveDocument.Content.End – 1;

Range rangeForTOC = adoc.Range(ref start, ref missing);

TableOfContents toc=adoc.TablesOfContents.Add(rangeForTOC, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

toc.Update();

Range rngTOC = toc.Range;

rngTOC.Font.Size = 10;

rngTOC.Font.Name = Georgia;

WordApp.Visible = true;

Bye…

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

19 thoughts on “Creating and Updating table of contents in word document using C#”

  1. Hi, i have see this post where you Create and Update table of contents in word document using C# (Version 2000). I wonder if this can be done for word document(Version 2007)? If that is possible can you post saome sample for helping me. Thanks.

    Like

  2. Hi i couldnt open the word document itself
    I m getting an exception ..i have pasted below

    Unable to cast COM object of type ‘Word.ApplicationClass’ to interface type ‘Word._Application’. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{00020970-0000-0000-C000-000000000046}’ failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

    Thank you
    Mano

    Like

  3. Hi Nishant,

    The code to generate TOC is working successfully, but the TOC is generated at the end of the page. Can you please help me How do i create and insert the TOC at the first page of the document.

    Thanking you
    Prakhyat Jain

    Like

  4. hi you can place the table of content in the document then traverse the document and find that particular table of content then you can update it.

    object missing = system.reflection.missing.value;
    microsoft.office.interop.word.application wordapp = new microsoft.office.interop.word.applicationclass();
    object readonly = false;
    object isvisible = false;
    object filename = “d:\\document.docx”;
    microsoft.office.interop.word.document adoc = wordapp.documents.open(ref filename, ref missing, ref readonly, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    adoc.tablesofcontents[1].update();
    adoc.save();
    adoc.close(ref missing, ref missing, ref missing);

    Like

  5. hi you can place the table of content in the document then traverse the document and find that particular table of content then you can update it.

    object missing = system.reflection.missing.value;
    microsoft.office.interop.word.application wordapp = new microsoft.office.interop.word.applicationclass();
    object readonly = false;
    object isvisible = false;
    object filename = “d:\\document.docx”;
    microsoft.office.interop.word.document adoc = wordapp.documents.open(ref filename, ref missing, ref readonly, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    adoc.tablesofcontents[1].update();
    adoc.save();
    adoc.close(ref missing, ref missing, ref missing);

    Like

  6. Im using VS 2005 Team Edition for Software Developer with Microsoft Office 2003
    I m getting an exception on wordApp.Document.Open(….. ..i have pasted below

    Unable to cast COM object of type ‘Word.ApplicationClass’ to interface type ‘Word._Application’. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{00020970-0000-0000-C000-000000000046}’ failed due to the following error: No such interface supported (Exception from HRESULT: 0×80004002 (E_NOINTERFACE)).

    Any Help

    Like

  7. hi, is it possible to display the contents of a table in word in text boxes using c#, such that each column is represented in the textbox of your project

    Like

  8. Hi,

    I have added the content to word document and from that content, depending on the style i need to create TOC dynamically. How can i do this? Please help me asap.

    Thanks in advance.

    Like

  9. Hello there! Do you use Twitter? I’d like to follow you if that would be okay.

    I’m definitely enjoying your blog and look forward to new updates.

    Like

  10. You are so awesome! I don’t suppose I have read something like this before.
    So good to discover somebody with genuine thoughts on this subject.
    Seriously.. many thanks for starting this up. This website is
    something that’s needed on the web, someone with a bit of
    originality!

    Like

  11. refer this –

    href=http://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/NET-Create-Table-Create-Word-Table-in-C-and-VB.NET.html

    Like

  12. I am not using the Default Heading Styles in my DOC. How I can Create the Table of content for the doc.
    I am creating reports in SSRS and Exporting it in the Word and then Merging the Word files.

    Like

Please share your thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Dynamics 365 Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM