none
Get TOC based on Paragraph section filtered on keywords RRS feed

  • Question

  • Need to get the Table of Contents for those whose Paragraphs for all the keyword/s that are supplied as search parameter are met.

    E.g: Search for Keywords like 'Documentation', 'Analysis' - This will get all the Table of Contents whose subsequent paragraphs has the keyword Documentation or Analysis in them.

    Then on selection of that particular Table of Contents (filtered list based on the searches) should display the complete section / paragraphs for that Table of Contents.

    Please suggest.

    Tuesday, January 13, 2015 3:13 AM

Answers

  • Hi SATISD9X,

    Seems that you want to show up a custom table which contains the TableOfContents items whose paragraphs include the keywords. I think the built-in TableOfContents will not meet your requirement. If you want to do this in VBA, you may need a UserForm to display the items. If it's a Word Add-in project, then a Windows Form or a UserControl could help.

    If you don't want to use any forms, it's very hard to find the TableOfContents items whoes paragraphs contains the keywords. The easiest way I can think of is to find the keywords and then highlight them. But it can't give you a list of the found TableOfContents items. If you want to do this, this code snippet should help:

    Sub FindAndHighlight()
        Application.ScreenUpdating = False
        Options.DefaultHighlightColorIndex = wdYellow
        With ActiveDocument.Content.Find
          .ClearFormatting
          .Text = "First"
          With .Replacement
            .Text = ""
            .ClearFormatting
            .Highlight = True
          End With
          .Forward = True
          .Wrap = wdFindContinue
          .Format = True
          .MatchWildcards = True
          .Execute Replace:=wdReplaceAll
        End With
        Application.ScreenUpdating = True
    End Sub


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by CaillenModerator Wednesday, January 14, 2015 9:59 AM
    • Marked as answer by SATISD9X Sunday, January 18, 2015 12:59 AM
    Wednesday, January 14, 2015 8:18 AM
    Moderator

All replies

  • Hi SATISD9X,

    Seems that you want to show up a custom table which contains the TableOfContents items whose paragraphs include the keywords. I think the built-in TableOfContents will not meet your requirement. If you want to do this in VBA, you may need a UserForm to display the items. If it's a Word Add-in project, then a Windows Form or a UserControl could help.

    If you don't want to use any forms, it's very hard to find the TableOfContents items whoes paragraphs contains the keywords. The easiest way I can think of is to find the keywords and then highlight them. But it can't give you a list of the found TableOfContents items. If you want to do this, this code snippet should help:

    Sub FindAndHighlight()
        Application.ScreenUpdating = False
        Options.DefaultHighlightColorIndex = wdYellow
        With ActiveDocument.Content.Find
          .ClearFormatting
          .Text = "First"
          With .Replacement
            .Text = ""
            .ClearFormatting
            .Highlight = True
          End With
          .Forward = True
          .Wrap = wdFindContinue
          .Format = True
          .MatchWildcards = True
          .Execute Replace:=wdReplaceAll
        End With
        Application.ScreenUpdating = True
    End Sub


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by CaillenModerator Wednesday, January 14, 2015 9:59 AM
    • Marked as answer by SATISD9X Sunday, January 18, 2015 12:59 AM
    Wednesday, January 14, 2015 8:18 AM
    Moderator
  • To build a Table of Contents along the lines you describe, the paragraphs concerned would need to be formatted in one or more Styles that aren't used for anything else in the document. The Table of Contents field could be configured to reference just those Styles. Such a Table of Contents would list the entire content of all paragraphs using that Style and, if you want, could include the page number. This approach could either result in a mess of your existing formatting or the duplication of many, if not all, of the Styles already used in the document.

    You might also consider whether what you want could be achieved via an Index or a keyword list.

    You can use Word's Indexing tools to build an Index. Clicking on an Index entry will take you to the relevant content.

    To build a keyword list that also includes the text in context would require a macro. Before such a macro could be coded, you would need to provide more detail about how your keywords are to be identified. For example, they might be in a table at either end of the document, or in another 'keyword' document, they could be expressions that appear in double-quotes in the document, or they could be hard-coded into the macro itself. Then there is the question of where you want the results output and how you want them displayed.

    You also need to clarify what you mean by 'complete section'. Sections in Word parlance are portions of a document delineated by Section breaks. If you mean something else, you need to provide specific advice on how your 'section' is to be defined.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, January 14, 2015 10:47 PM
  • Thanks for your reply Paul. Please find the same query with diff content - http://forums.asp.net/p/2031318/5847072.aspx?Get+the+list+from+the+XML+provided
    Friday, January 23, 2015 5:05 AM
  • In your link, it appears you want three elements from the document, viz:
    TOCNum, TOC, Description
    for which an example is:
    Toc409454824, 1.1 Alumina, Aluminum oxide

    Your 'TOCNum' entry (Toc409454824) is just the bookmark name applied by the TOC field to the corresponding entry '1.1 Alumina'. These bookmarks IDs are quite ephemeral and are liable to change every time the TOC is refreshed. I wouldn't be inclined to place too much reliance on them.

    Your 'TOC' appears to reference a paragraph in Heading 2 Style, containing the text '1.1 Alumina'. It is quite easy to build such a list, perhaps the simplest method being to insert a TOC field and convert its output to text. One can retrieve the TOC bookmarks at the same time. For example, you might use a macro coded like the following to insert the list at the end of the document:

    Sub CompileTOCRefs()
    Application.ScreenUpdating = False
    Dim RngTOC As Range, StrBkMkList As String, i As Long
    With ActiveDocument
      .Range.InsertAfter vbCr
      Set RngTOC = .Paragraphs.Last.Range
      .Fields.Add Range:=RngTOC, Type:=wdFieldEmpty, Text:="TOC \o 2-9 \h \n", PreserveFormatting:=False
      With .TablesOfContents(ActiveDocument.TablesOfContents.Count)
        For i = 2 To .Range.Fields.Count
          StrBkMkList = StrBkMkList & "|" & Split(Trim(.Range.Fields(i).Code.Text), " ")(2)
        Next
        Set RngTOC = .Range
      End With
      RngTOC.Fields.Unlink
      For i = 1 To UBound(Split(StrBkMkList, "|"))
        RngTOC.Paragraphs(i).Range.InsertBefore Split(StrBkMkList, "|")(i) & vbTab
      Next
    End With
    RngTOC.InsertBefore "TOCNum" & vbTab & "TOC" & vbTab & "Description" & vbCr
    Application.ScreenUpdating = True
    End Sub

    It is not apparent where in the document your 'Description' text comes from, so I can't comment yet on how you'd retrieve that.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, January 23, 2015 7:00 AM
  • Thanks Paul. Will take a look at this.
    Tuesday, April 14, 2015 4:19 AM