Free Support Forum - aspose.com

Table of Contents and Macros

I have been looking for a way to impliment a table of contents with page numbers. Is this possible with the most recent version of Aspose.Word? I have read many forum posts about this, but none of them seem to help.

The function InsertTableOfContents() doesn't display anything. I beleive this is because I have not marked TOC anchors correctly. Furthermore, after getting the latest version (2.5.4) every place where my Header 1 sections are, the text {TC "I. Introduction" \f C \l "1"} appears.

I have also tried using a Document_Open() macro to update an existing TOC in the template. This method, while properly displaying the contents of the export, still does not update the table of contents. The macro doesn't run when the document opens and I get an error when I try to run the macro. This is the error message: "The macros in this project are disabled. Please refer to the online help or documentation of the host application to determine how to enable macros." Here is the macro that I am trying to use:



Private Sub Document_Open()
'This macro will run when the word document first opens

'Update the table of contents
UpdateTOC
End Sub
Sub UpdateTOC()
'Application.Assistant.DoAlert "UpdateTOC", "UpdateTOC", msoAlertButtonOK, msoAlertIconInfo, msoAlertDefaultFirst, msoAlertCancelFirst, False
With ActiveDocument.TablesOfContents(1)
.UpdatePageNumbers
.Update
ActiveDocument.Repaginate
.Update
.Range.ParagraphFormat.Reset
End With
End Sub


I have attached a sample word document.
Here is the code that was used to create the TOC using InsertTableOfContents:


If Options.TableOfContents Then

mBuilder.MoveToMergeField(“TableOfContents”)

mBuilder.InsertTableOfContents("\o ““1-2"” \h \z”)

End If






Hi,

Thank you for considering Aspose.

To run the macro that updates the TOC you should enable macros in the Microsoft Word settings as the error message recommends. I opened your document and the macro ran so the TOC has got updated.

In respect of InsertTableOfContents, it is surely works becuause it just inserts a TOC field with the switches you specify.

You also wrote that “after getting the latest version (2.5.4) every place where my Header 1 sections are, the text {TC “I. Introduction” \f C \l “1”} appears”. Could you please clarify this a bit more and attach the original template so we could test it and see the result?

Sure… Somehow, Aspose.Word is taking my TC fields and converting them to regular text. Then, when my TOC updates, it says "Error, no TOC entries"
The result is attached.

Oh, and I got my macros to work okay. Thanks.

And attached is the template that was used to generate that word file.
Note: Some sections of the template are removed progamatically using Sections.RemoveAt()

Thanks for the report, we will try to fix TC fields handling asap.

TC fields reading and writing are fixed in forthcoming Aspose.Word v3.0.