none
Create a TOC in an RTF file RRS feed

  • Question

  • Cindy - I'm having exactly the same issue as Dane  with Table of Contents creation.  I am generating an RTF document and I want the Table of contents to look right when it's opened in Word.  At the moment, I use this line to add the TOC to the RTF document:

    {\\field\\flddirty\\fldedit{\\*\\fldinst TOC}{\\fldrslt Update Field (right-click in MS Word) to show Table of Contents}}

    That products a grey area containing the text "Update Field (right-click in MS Word) to show Table of Contents".  If they right-click on this grey text and choose "Update Field" from the dropdown menu that appears, they get a proper Table of Contents.  But  obviously what I really want is for the TOC to be right first time when they open the document.  Is there anything I can specify in the RTF file itself that will cause the TOC to be updated automatically?  Because if not, how are you supposed to generate a TOC for Word (or an Index for that matter) using RTF?

    Sorry if I'm posting this in the wrong forum.  I appreciate that this is probably more of an RTF question than a Word one, but I can't find an RTF forum to post it in.  Any help much appreciated.


    Simon
    • Split by Cindy Meister MVPModerator Monday, September 12, 2011 10:03 AM Split from http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/edb16f20-95c1-4c66-bae0-bf11f14fed49/ for better visibility and topic-relationshaip
    Saturday, September 10, 2011 8:45 AM

All replies

  • Hi Simon

    The same answer, really: there's nothing you can set in a document that will force this to update when the document is opened in Word. You'd need some kind of macro or other code if the user shouldn't have to do it manually.

    FWIW, unless the option is turned off, the field should update when the document is printed. Not that the helps, I suppose...


    Cindy Meister, VSTO/Word MVP
    Saturday, September 10, 2011 10:40 AM
    Moderator
  • Cindy - OK I understand that there's nothing I can put in the RTF document that will get Word to automatically update the Table-of-contents field when the RTF document is opened.  But there must be some way for me to 'manually' do the same thing.

    I just created a very simply Word document with 2 heading lines on 2 pages, and a very small table of contents.  I saved that file in RTF format.  Then I re-opened it in Word.  The Table-of-contents was there looking perfect.  So how did Word do that? Because that's what I want to do. 

    I have looked at the RTF spec and it doesn't tell you anything about it as far as I can see.  It just barely even mentions that you can insert a field called "TOC".  It doesn't begin to tell you how to 'manually' make this field look like an entire table of contents.  And yet that is what somehow happens in Word.  Word hasn't simply created an ordinary table that looks like a TOC.  If I right-click on the TOC it acts like a single field and can be updated like a single field.

    Incidentally, I did of course look at the actual RTF generated by Word, to see if I could work out from that how it does it.  Unfortunately I can't.  The Word-generated RTF is tortuously complicated to say the least, and extremely verbose; and without some spec indicating which control words do what, and what you're supposed to do, I don't think there's any way to work it out from that.

    I appreciate that this question is more RTF than Word, but there is no RTF forum.  When I looked up where to post RTF questions, this forum was given as one of the answers.

    P.S. All the same issues apply to the Index incidentally.


    Simon
    Monday, September 12, 2011 9:21 AM
  • Hi Simon

    When you save a document in the RTF file format, then the TOC - as it was generated - is saved with it. This would be the result of a field code (as opposed to the TOC field code, which will also be present).

    But it's certainly not done in the way you wish, that there's something in the RTF that triggers the TOC being updated when the document is opened.

    Here's some information on the RTF file format specs:
    http://msdn.microsoft.com/en-us/library/aa140277(office.10).aspx
    http://support.microsoft.com/kb/86999

    Please note, however, that this will not be extended or supported any more. A couple of years ago, it was discontinued since the Open XML file format is open source and humanly readable (which was the whole reason behind RTF in relation to Word).


    Cindy Meister, VSTO/Word MVP
    Monday, September 12, 2011 9:28 AM
    Moderator
  • Hi Cindy - Thanks for your help with this.  I do appreciate that what I had originally asked for (a way of getting Word to do all the hard work for me) can't be done.  And I also appreciate that what is being saved in the RTF file is the result of a field code being updated.  I understand that.  But nevertheless, whatever it is, it is valid RTF.  And if Word can generate it, I should be able to do it too. 

    I supposed what I'm asking is this:

    How can I generate RTF in such a way that you get something which looks like a table-of-contents (even if the page numbers are wrong), and which behaves like a TOC field - i.e. it can be updated by the user by clicking on it and choosing the "Update Field" command.

    I appreciate that I can generate an ordinary table of some kind.  But (a) it's fiddlier than you might think because you need it to be nicely justified right and left, and you want all those little dots to fill the spaces between the headings on the left and the page numbers on the right (how do you do that?  Word manages somehow); and (b) if I just used an ordinary table,  it wouldn't be a field, and there would be no way for the user to update it by right-clicking on it and choosing the "Update Field" command.

    Clearly the thing is possible, but I can't find anything about it in the RTF spec.  I looked in the links you gave.  They have a long section on "Table of Contents Entries" which look relevant but aren't.  This section looks like it's about how to generate a TOC table manually, but it isn't.  It is actually all about how to insert code into the main body of your document, which will be picked up by the TOC field when that field is updated, if you don't want it (the TOC field) to just work from ordinary heading styles.  But I do want the TOC field to use ordinary heading styles, so this section is not relevant to me (and I note that Word doesn't use any of the stuff in this section either).

    What Word is doing must be documented somewhere.  The question is where.  Does anyone know?  My apologies if it's in either of the documents you gave me a link to, and I missed it.  If it is, please do tell me where to look. 


    Simon
    Monday, September 12, 2011 9:49 AM
  • Just to clarify a little further.  In my original post, I mentioned the RTF I'm using which uses the fldrslt control code.  This must a key part of it - and sure enough I notice that Word does seem to use this code a  lot in its generated RTF.  But it's how you're supposed to use it, to get the required TOC look, that I can't work out.


    Simon
    Monday, September 12, 2011 9:55 AM
  • Hi Simon

    I'm afraid I've helped you as far as I can. I never was very familiar with RTF so have never studied it.

    There are a couple of people who work on-line who do have some knowledge. One of them is Graham Mayor. He drops in here only occasionally, though. You're more likely to be able to find him at:
    http://answers.microsoft.com/en-us/office/forum/word

    And I think he also has a website.

    Since your question is different from that of the original OP and the contributiosn revolving around your topic have "ursurped" the original discusison, I'm going to split this part off into an independent thread. You should look for that and maybe provide a link for Graham (or anyone else who might respond in Answers).


    Cindy Meister, VSTO/Word MVP
    Monday, September 12, 2011 10:01 AM
    Moderator
  • Please update this post with the split.   Simon is right on the money.  Appreciate his patience and verbosity explaining it in so many different ways.    Word has to be able to do this.   Don't thin it is a RTF issue.

    Don't give up!  Please keep me in the loop!!!

    Graham might be able to help.  

    Cindy, appreciate your input as well.  Do you have access to internal people to get this addressed?

     


    Dane!
    Monday, September 12, 2011 3:00 PM
  • Hi Dane

    <<Do you have access to internal people to get this addressed?>>

    Not really, and not in the way you mean.

    This is an issue of updating fields. Not only the TOC field is "static" in this manner. Almost all fields, except those in the header/footer, won't update when a document is opened. This is by design, has always been this way and is, in part, a security issue, as well as a resource issue. (Used to be, links to outside files could be forced to update when a document was opened, but that was discontinued a few years ago).

    Here are a couple of articles that describe the "philosophy" behind when which fields update:
     "WD97: Fields Updated When Document Repaginated"

    http://support.microsoft.com/?kbid=89953

     

    "The FILENAME field does not automatically update when you

    open a document in Word 2002 or Word 2003"

    http://support.microsoft.com/?kbid=832897

     

    The workaround Simon is now considering is to generate the TOC for the field display manually (IOW, put the text in the field result). He's just not been able to find out the required syntax because what he's seeing in the RTF is so complex. Perhaps the two of you (and if someone can turn up Graham and the problem interests him) can do some creative detective work :-)

     

    Another thing to consider would be migrating from RTF to the Office Open XML file format. For that, at least, you should be able to get support in figuring out the required syntax. and the XML code will probably also be more "human readable" than the RTF.


    Cindy Meister, VSTO/Word MVP
    Tuesday, September 13, 2011 9:22 AM
    Moderator
  • Hi Dane,

    How about the problem on your side? Does the suggestion help you? If you still need any assistant, just feel free to let us know.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, September 19, 2011 8:31 AM
  • Hi Bruce and Cindy (and Simon).

    Thanks for following up.  For now, the issue is a 'bug' on our side and has been put into a "deferred" state pending more time to address the issue.

    I am not the developer, but am the IT guy and trying to help them out.

    Really appreciate all of Cindy's time and input. 

    It seems like the table of contents does not work on open because it was deemed to be a security risk.   Well, the mechanism for it to work automatically.

    Of the solutions proposed -

    Use Office Services from Sharepoint

    Simon wants to put some field somewhere or a button, I don't really understand it.

    Switch to Office OpenXML ?  I presume this would create an xdoc?  There must be a reason we generate RTF - I don't know why off hand but have an idea.  This might work if the Office services from sharepoint won't work..?

    I like the first one, using Office Services from Sharepoint.    My question is, how do we do this?  Is this something that can be extracted from a sharepoint deployment?   Do I need a sharepoint server in the background (which is OK)?    Can this be done with Sharepoint Services (the 'free' version of Sharepoint as opposed to the full blown version?)   ...  Guess this was in the previous post before it was split.

     


    Dane!
    Tuesday, September 20, 2011 5:34 AM
  • Hi Simon,

     

    My name is Ross and I am part of the Microsoft Word team. Would it be possible for you to share a document that is having this issue with me so I can get a little better visualization of your issue?

     

    Thank you,

    Ross

    Wednesday, September 21, 2011 5:46 PM
  • Hi Ross and everyone

    Thank you all for your posts and sorry for not responding before now.  In fact I have found an adequate solution to the problem (good enough for my purposes anyway), so I thought I'd post it in case anyone out there is wrestling with the same problem.

    Previously I was outputting this for the table-of-contents (see my first post in this thread):

    {\\field\\flddirty\\fldedit{\\*\\fldinst TOC}{\\fldrslt Update Field (right-click in MS Word) to show Table of Contents}}

    Now I 'manually' generate a reasonable approximation of the T.O.C. which I insert instead of the text "Update Field (right-click in MS Word) to show Table of Contents".  The key thing was working out how to get the dotted lines to connect the left text with the page numbers right-aligned.  To do that, I use lines like this:

    \par\pard \ql\li0\widctlpar\adjustright\plain\f21\fs20\cf23 1.  First Chapter\tqr\tldot\tx10460\tab 1
    \par\pard \ql\li0\widctlpar\adjustright\plain\f21\fs20\cf23 2.  Second Chapter\tqr\tldot\tx10460\tab 2
    \par\pard \ql\li0\widctlpar\adjustright\plain\f21\fs20\cf23 3.  Third Chapter etc\tqr\tldot\tx10460\tab 4

    When a user generates an RTF file I warn them that the file contains a table-of-contents and/or index and that the page numbers displayed are out-of-date and they need to refresh them (e.g. by using MS Word's "Update Field" command).  It isn't perfect.  Ideally I'd still like the RTF to automatically refresh the TOC and index somehow.  But I can live without that.


    Simon

    Tuesday, July 10, 2012 9:52 AM