none
Automatically generating bookmarks from styles? RRS feed

  • Question

  • In Word 2007 (and VBA), is it possible to automatically generate bookmarks for all headings (styles H1, H2, H3, etc) in a document, using the heading text as the name of the bookmark?  I.e., without having to go back and manually insert a bookmark for each one?

    And/or, could a bookmark be set up as part of the heading styles, so when you format text to be H1, for example, it would automatically have a bookmark attached to it (with the heading text being the name of the bookmark)?

    Thanks!

    Thursday, May 5, 2011 5:32 PM

Answers

  • It would be possible to create a macro that could be run to insert bookmarks around the heading text, but the text would have to conform to the bookmark naming conventions and as a minimum, the code would need to use the Replace() function to remove or replace any spaces in the text of the heading.

    What however is the reason for wanting to do it?  There may be other methods of achieving the same thing.


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "dogwoodnc" wrote in message news:c88be70b-fca6-4f00-b6cd-b07613820baa@communitybridge.codeplex.com...

    In Word 2007 (and VBA), is it possible to automatically generate bookmarks for all headings (styles H1, H2, H3, etc) in a document, using the heading text as the name of the bookmark?  I.e., without having to go back and manually insert a bookmark for each one?

    And/or, could a bookmark be set up as part of the heading styles, so when you format text to be H1, for example, it would automatically have a bookmark attached to it (with the heading text being the name of the bookmark)?

    Thanks!


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Thursday, May 5, 2011 7:31 PM
  • Hi Dogwood,

    Below is a code snippet from one of my apps.  We have documents based on an organizing principle of Modules and Lessons. Modules are Heading 1 and Lessons are Heading 2.  We bookmark “Module Titles” for use in other areas.  Bookmark names must be unique and cannot contain spaces so we create our own names.  I didn’t include all of the variable declarations, but hopefully by their names and how they’re being used it will be clear.

    We use a document variable to store the number of “Module Titles” in the document. With that and our standard prefix for Bookmark names we can later access all of them easily.

    Hope this helps

     

            done = False

            wApp.Selection.Find.ClearFormatting()

            wApp.Selection.Find.Style = wApp.ActiveDocument.Styles("Heading 1")

            Do While Not done

                'find next Module title

                With wApp.Selection.Find

                    .Text = ""

                    .Format = True

                    .Forward = True

                    .Wrap = wdFindStop

                    .Execute()

                End With

                'if one is found, add bookmarks

                If wApp.Selection.Find.Found Then

                    Call IncrementNumberOfModules()

                    ModNumber = Globals.ThisAddIn.getDocVariable("NumberOfModules")

                    'AddBookmark "ModuleName" & CStr(ModNumber)

                    str = "ModuleName" & CStr(ModNumber)

                    wApp.ActiveDocument.Bookmarks.Add(str)

                    wApp.Selection.Collapse(wdCollapseEnd)

                 Else

                    done = True

                 End If

           Loop


    Regards, Rich
    Friday, May 6, 2011 3:56 AM

All replies

  • It would be possible to create a macro that could be run to insert bookmarks around the heading text, but the text would have to conform to the bookmark naming conventions and as a minimum, the code would need to use the Replace() function to remove or replace any spaces in the text of the heading.

    What however is the reason for wanting to do it?  There may be other methods of achieving the same thing.


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "dogwoodnc" wrote in message news:c88be70b-fca6-4f00-b6cd-b07613820baa@communitybridge.codeplex.com...

    In Word 2007 (and VBA), is it possible to automatically generate bookmarks for all headings (styles H1, H2, H3, etc) in a document, using the heading text as the name of the bookmark?  I.e., without having to go back and manually insert a bookmark for each one?

    And/or, could a bookmark be set up as part of the heading styles, so when you format text to be H1, for example, it would automatically have a bookmark attached to it (with the heading text being the name of the bookmark)?

    Thanks!


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Thursday, May 5, 2011 7:31 PM
  • Thanks!  The reason is that we have thousands of docs formatted with H1-H4 heading styles, but no bookmarks associated with any of them.  The docs are housed in a repository that allows us to link from one to the other, but at the moment, since there are no bookmarks, we can't link to a specific section within each doc -- which we'd like to do.  Manually coding the thousands and thousands of existing H1-H4 headings would be extremely time-consuming, so we're looking for an alternative method.  ANY additional ideas/suggestions would certainly be appreciated; we may be too close to it to see other options! :)

    Thursday, May 5, 2011 9:20 PM
  • Hi Dogwood,

    Below is a code snippet from one of my apps.  We have documents based on an organizing principle of Modules and Lessons. Modules are Heading 1 and Lessons are Heading 2.  We bookmark “Module Titles” for use in other areas.  Bookmark names must be unique and cannot contain spaces so we create our own names.  I didn’t include all of the variable declarations, but hopefully by their names and how they’re being used it will be clear.

    We use a document variable to store the number of “Module Titles” in the document. With that and our standard prefix for Bookmark names we can later access all of them easily.

    Hope this helps

     

            done = False

            wApp.Selection.Find.ClearFormatting()

            wApp.Selection.Find.Style = wApp.ActiveDocument.Styles("Heading 1")

            Do While Not done

                'find next Module title

                With wApp.Selection.Find

                    .Text = ""

                    .Format = True

                    .Forward = True

                    .Wrap = wdFindStop

                    .Execute()

                End With

                'if one is found, add bookmarks

                If wApp.Selection.Find.Found Then

                    Call IncrementNumberOfModules()

                    ModNumber = Globals.ThisAddIn.getDocVariable("NumberOfModules")

                    'AddBookmark "ModuleName" & CStr(ModNumber)

                    str = "ModuleName" & CStr(ModNumber)

                    wApp.ActiveDocument.Bookmarks.Add(str)

                    wApp.Selection.Collapse(wdCollapseEnd)

                 Else

                    done = True

                 End If

           Loop


    Regards, Rich
    Friday, May 6, 2011 3:56 AM
  • Thanks; I'll give that a try too!
    Friday, May 6, 2011 3:15 PM