Consulting

Results 1 to 4 of 4

Thread: Create automatic index in Word based on Headings with VBA

  1. #1

    Post Create automatic index in Word based on Headings with VBA

    Hi,

    I've made family cookbook in Word and with every new item I'm able to renew the TOC with that new item (recipe). The items in the TOC also have hyperlinks to the specific recipe and every Heading has a hyperlink, that brings you back to the TOC. However, all the recipes are added after the last recipe and this makes the TOC difficult to read.
    I would like to create an alphabetical index at the end of my document, with all the headings of recipes and respective pages, and the index items also hyperlinked to the recipes.
    I'm a bit at a loss how to do this. I can mark a heading for an index. But how to do this in VBA? Also to mark the index item as an hyperlink?

    I would appreciate your help.

    I've added the TOC macro below:
    Sub UpdateTOC()
    ' Update TOC and hyperlinks Macro
    Application.ScreenUpdating = False
    Dim hyp As Hyperlink
    Dim toc As TableOfContents
    Dim k As Long
    Dim bkmk As String
    Dim sCode As String
    Dim fld As Field
    Dim aRange As Range
    If ActiveDocument.TablesOfContents.Count = 0 Then
        MsgBox "There are no Tables of Contents in document"
        Exit Sub
    End If
    If ActiveDocument.TablesOfContents.Count = 1 Then _
       ActiveDocument.TablesOfContents(1).Update
       Set toc = ActiveDocument.TablesOfContents(1)
       For Each fld In toc.Range.Fields
          sCode = fld.Code.Text
          If InStr(sCode, "HYPERLINK") > 0 Then
    bkmk = Mid(sCode, InStr(sCode, "_"))
    bkmk = Left(bkmk, Len(bkmk) - 2)
    fld.Select
    ActiveDocument.Bookmarks.Add Range:=Selection.Range, _
    Name:=bkmk & "R" Set aRange = ActiveDocument.Bookmarks(bkmk).Range
    aRange.Select
    With ActiveDocument.Hyperlinks.Add(Anchor:=Selection.Range, _
    Address:="", SubAddress:=bkmk & "R", _
    TextToDisplay:=Selection.Text)
    .Range.Select
    Selection.ClearCharacterAllFormatting
    End With End If Next fld Options.CtrlClickHyperlinkToOpen = False
    Selection.GoTo What:=wdGoToBookmark, Name:="Inhoud" Application.ScreenUpdating = True End Sub
    Last edited by Aussiebear; 03-05-2023 at 06:04 AM. Reason: Added code tags to wrap the submitted code

  2. #2
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    123
    Location
    The Table of Contents and the Index are different fields and the Index field does not have hyperlink capability built into it.
    I believe you can find macros to assign hyperlinks to an Index in the Word vba forum.

  3. #3
    Quote Originally Posted by Chas Kenyon View Post
    The Table of Contents and the Index are different fields and the Index field does not have hyperlink capability built into it.
    I believe you can find macros to assign hyperlinks to an Index in the Word vba forum.
    Dear Chas,
    Thanks for replying. I know that TOC and Index are different. The macro I submitted makes an automatic TOC with hyperlinks. However, since the added recipes are not added in alphabetical order, it becomes difficult to search. Therefor I would like to add an Index. This is automatically alphabetical. But I'm looking for a macro to transform every heading into an Index item and then create an Index with hyperlinks.
    If you know where to find the macro to assign hyperlinks to an Index...?
    Greetings, johan

  4. #4
    Hi everyone, I could use some more help on this one...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •