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…
awesome..
LikeLike
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.
LikeLike
The TOC is crreating in document but not working
LikeLike
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
LikeLike
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
LikeLike
It´s at the end because of the Range . If you want it in the beginning,the rangeForTOC should be like this: rangeForTOC = doc.Range(0, 0);
LikeLike
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);
LikeLike
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);
LikeLike
I want to align the words in center using the dis code… how can i do it…
LikeLike
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
LikeLike
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
LikeLike
I want to Add TOC section wise in Word Document
So how can i do this ???
LikeLike
Hey if anyone is interested in getting the TOC content, they can refer my answere here
http://social.msdn.microsoft.com/Forums/en-SG/worddev/thread/61dc6ebd-d9b6-472e-9762-5923f0e10510
LikeLike
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.
LikeLike
Check out this forum post to know more about how you can add table of content in your word file using c#:
http://www.aspose.com/community/forums/permalink/35508/35508/showthread.aspx
LikeLike
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.
LikeLike
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!
LikeLike
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
LikeLike
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.
LikeLike