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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

CRM for You and Me

Customer Relationship Mgt App for the Professional

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

Two Bite Tips

Valuable Tips To Grow Your Business

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

EVOLVED365

Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

Sara Lagerquist

No Code Customization Concepts

innovativeaj

developer, designer, writer, fun loving, patriotic, humble and a sweet person inside out :) love and respect people who have a great sense of humor.

Temmy Wahyu Raharjo

Dreaming to be a clean coder and TDD minded programmer.

Transform 365

We blog about problems we face and code we write to help others

%d bloggers like this: