none
how to make a toc has hyperlink RRS feed

  • Question

  • hi, everybody

    i want to programmatic insert a toc, but the result toc has no hyperlink which like below picture.

    my code is:

    Word.Field fld = doc.Fields.Add(rng, Word.WdFieldType.wdFieldTOC, "test");
                fld.Update();

    if insert toc by default ribbon UI command, each item in toc with hyperlink,

    can anyone tell me the reason about it? then how can i do ?

    Tuesday, March 11, 2014 10:13 AM

Answers

  • Sendreams,

    Try replacing:
    Word.TableOfContents TOC = doc.TablesOfContents.Add(doc.Characters.Last, False, 1, 9, False, Null, Null, False, Null, False);
    //toc.UseHyperlinks = true;
    //toc.IncludePageNumbers = false;

    with:
    Word.TableOfContents TOC = doc.TablesOfContents.Add(doc.Characters.Last, True, 1, 9, False, False, False, False, False, True, True, False);

    Peter: The code being used inserts a TOC with hyperlinks, so there must be a sub address for each entry.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Wednesday, March 12, 2014 4:34 AM
    • Marked as answer by sendreams Wednesday, March 12, 2014 9:01 AM
    Tuesday, March 11, 2014 8:52 PM

All replies

  • hi, everybody

    i want to get all of the heading in document programmatic, may be can use toc item, but when i going to deep, i met some  problem , my inqirement with some special feature:

    1, if use toc, i need a background toc which not render in document and can not persist. because i just need the headings.

    2, if use range.find method, just can search one style heading each time, but i need keep all the heading's orignal sequence.

    any idea for this?

    thanks


    Tuesday, March 11, 2014 8:53 AM
  • You could insert the TOC, then delete it after you've finished with it. There is no need to leave it in the document.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, March 11, 2014 9:01 AM
  • hi, macropod

    thanks a lot.

    i look the Word.TableOfContents's api, perheps just can get the heading's heading-text, but i want the Target item which is a Word.Paragraph object.

    can do it?

    Tuesday, March 11, 2014 9:06 AM
  • my code is like below:

    Word.Field fld = doc.Fields.Add(rng, Word.WdFieldType.wdFieldTOC);
                fld.Update();
                Word.TableOfContents toc = doc.TablesOfContents[1];
                foreach (Word.Hyperlink hl in toc.Range.Hyperlinks) {
                    // can not get the relative target Heading item(Word.Paragraph object)
                }
                fld.Delete();

    Tuesday, March 11, 2014 9:10 AM
  • i look the Word.TableOfContents's api, perheps just can get the heading's heading-text, but i want the Target item which is a Word.Paragraph object.

    It would have been helpful if you said what you wanted at the outset. Your initial post here and in the other thread referred only to 'getting' all the headings. I'm still not sure what you mean by "i want the Target item which is a Word.Paragraph object". It seems to their ranges, but there are other things you can retrieve as well. For example:

    Sub Demo()
    Dim TOC As TableOfContents, RngTOC As Range, RngHd As Range, i As Long
    With ActiveDocument
      Set TOC = .TablesOfContents.Add(Range:=.Characters.Last, UseHyperlinks:=True, IncludePageNumbers:=False)
      With TOC.Range
        For i = 1 To .Paragraphs.Count - 1
          Set RngTOC = .Paragraphs(i).Range
          Set RngHd = ActiveDocument.Bookmarks(RngTOC.Hyperlinks(1).SubAddress).Range
          MsgBox "The heading:" & vbCr & _
            RngTOC.Text & _
            "is numbered: " & RngHd.ListFormat.ListString & vbCr & _
            "Spans  from: " & RngHd.Start & " to: " & RngHd.End
        Next
      End With
      TOC.Delete
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Tuesday, March 11, 2014 9:57 AM
    Tuesday, March 11, 2014 9:57 AM
  • hi, macropod

    thanks again and say sorry for the unclear introduce about the question.

    i run your code and i think it helpful for me, i will test later and tell you the result.

    "i want the Target item which is a Word.Paragraph object". i mean is:  want to get the heading relative COM object for interaction.

    thanks.

    Tuesday, March 11, 2014 10:34 AM
  • The code I gave you in this thread: http://social.msdn.microsoft.com/Forums/office/en-US/45056df0-7fb7-4dd5-9e14-e4c9e5304abb/how-to-quickly-get-all-of-the-headings?forum=worddev shows how to add the hyperlink parameter to a TOC.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, March 11, 2014 12:29 PM
  •  i translate you VBA code to C# below

    Word.TableOfContents toc = doc.TablesOfContents.Add(doc.Characters.Last, false, 1, 9, false, null, null, false, null, false);
    //toc.UseHyperlinks = true;
    //toc.IncludePageNumbers = false;
    StringBuilder sb = new StringBuilder();
    for (int i=1;i<toc.Range.Paragraphs.Count-1;i++) {
    	Word.Range TocRng = toc.Range.Paragraphs[i].Range;
    	string name = TocRng.Hyperlinks[1].SubAddress;   // will throw error here
    	Word.Range headingRng = doc.Bookmarks[name].Range;
    	sb.Append(headingRng.Text + "——" + headingRng.ListFormat.ListString);
    	sb.Append(" Range [" + headingRng.Start + "-" + headingRng.End + "]");
    	sb.Append("\r");
    }
    MessageBox.Show(sb.ToString());

    but this code will throw exception at runtime.

    pls look the error picture:

    anything go wrong?

    Tuesday, March 11, 2014 1:00 PM
  • the VBA code could run perfect, i don't understand why.
    Tuesday, March 11, 2014 1:02 PM
  •  i translate you VBA code to C# below
    ...

    but this code will throw exception at runtime.

    I can't help you with that - I don't know enough C# to scratch myself with.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, March 11, 2014 1:06 PM
  • hi, macropod

    thank you for your reply

    but after run your code the result which is like the picture1 other than the picture2.

    after record a macro then get the VBA code below:

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    '
        Application.Templates( _
            "C:\Users\CHN\AppData\Roaming\Microsoft\Document Building Blocks\1033\15\Built-In Building Blocks.dotx" _
            ).BuildingBlockEntries("Automatic Table 1").Insert Where:=Selection.Range _
            , RichText:=True
    End Sub

    but if i change this vba code to c# below:

    Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
    Word.Range rng = doc.ActiveWindow.Selection.Range;
    Word.BuildingBlock bb = Globals.ThisAddIn.Application.Templates[1].BuildingBlockEntries.Add("toc", Word.WdBuildingBlockTypes.wdTypeTableOfContents, "test", rng);

    i can not see any effact in document.

    so i ask this question for help.

    thanks


    Tuesday, March 11, 2014 1:14 PM
  • i have tried change some parameter in doc.TablesOfContents.Add api, but can not have any hyperlink finally.
    Tuesday, March 11, 2014 1:17 PM
  • i have been translate VBA to C# serveral time, every time is ok but today i have being cracked down two time.

    can you please deliver this thread to any other expert, thanks

    Tuesday, March 11, 2014 1:30 PM
  • my envirement is:

    vs2013 sp1, office2013 sp1 64, win8 ent

    Tuesday, March 11, 2014 1:32 PM
  • Assuming that there are hyperlinks in the specified range (you can check that .Hyperlinks.Count for the range > 0) then the most likely problem is that there is no SubAddress in the specified link. In that case, VBA just returns a blank string, but C# throws an exception instead.

    I think.

    Peter Jamieson

    Tuesday, March 11, 2014 4:45 PM
  • Sendreams,

    Try replacing:
    Word.TableOfContents TOC = doc.TablesOfContents.Add(doc.Characters.Last, False, 1, 9, False, Null, Null, False, Null, False);
    //toc.UseHyperlinks = true;
    //toc.IncludePageNumbers = false;

    with:
    Word.TableOfContents TOC = doc.TablesOfContents.Add(doc.Characters.Last, True, 1, 9, False, False, False, False, False, True, True, False);

    Peter: The code being used inserts a TOC with hyperlinks, so there must be a sub address for each entry.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Wednesday, March 12, 2014 4:34 AM
    • Marked as answer by sendreams Wednesday, March 12, 2014 9:01 AM
    Tuesday, March 11, 2014 8:52 PM
  • I think this will do it.

    Split

    	string name = TocRng.Hyperlinks[1].SubAddress;   // will throw error here
    

    into 2 lines:

    Word.Hyperlink hl = TocRng.Hyperlinks[1];
    string name = hl.SubAddress;  
    

     


    Peter Jamieson

    • Proposed as answer by Harsh Porecha Saturday, October 17, 2015 8:23 AM
    Tuesday, March 11, 2014 9:18 PM
  • <<
    Peter: The code being used inserts a TOC with hyperlinks, so there must be a sub address for each entry.
    >>

    OK.


    Peter Jamieson

    Tuesday, March 11, 2014 9:22 PM
  • hi, macropod

    thank you very much

    the exception was gone. hah

    thanks

    Wednesday, March 12, 2014 9:01 AM
  • Thanks Peter Jamieson.

    Works perfect for me !!

    Saturday, October 17, 2015 8:23 AM