0

How to make a style as a bookmark in word 2010?

enter image description here

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Amar
  • 65
  • 1
  • 2
  • 8
  • I made a code to select headers individually and make them as a bookmark. But even that takes a help lot of work. I have around 500 headings to be bookmarked in a document. And I have many documents to bookmark still – Amar Jul 05 '12 at 16:34
  • 1
    You won't find much help unless you post some code you're trying to work on and/or fix. Generally Stack Overflow will not write a whole program for you. – Alex Jul 05 '12 at 19:25
  • What have you tried to implement using the code you've picked up from [this question](http://stackoverflow.com/q/11333817/698590)? – Gaffi Jul 05 '12 at 19:36
  • What are you trying to accomplish? Sometimes there are easier, if not already ways to do that. – ForEachLoop Jul 05 '12 at 20:22
  • @user1502215 If you need to insert a Bookmark at each Paragraph which has a particular Style applied (or set of styles such as Word's built-in "Heading" styles), then edit your question to reflect this requirement, and post relevant code. I see some more details in the image you posted up - but this needs to be in the question description so people don't miss it. Put some more time in how you ask the questions; I guarantee you'll get better answers. – JohnZaj Jul 06 '12 at 00:48
  • Was this one of your questions, too?: http://stackoverflow.com/questions/9522429/vba-word-bookmarking it may be best to create an account so we can see patterns of your questions to help us better answer them – JohnZaj Jul 06 '12 at 05:16
  • @user1502215 you won't be able to create bookmark names with these special characters e.g. "(" and ")". Word simply won't allow it (you can see this by doing an Insert Bookmark and typing in one of these characters...you won't be able to add). One option you have is to simply strip these characters from the bookmark name before adding it. If this will work, let me know..sure I'll post code for you – JohnZaj Jul 06 '12 at 05:31

1 Answers1

1

You won't be able to use most of the text in the document as the bookmark name. It is just illegal to use certain characters in a bookmark name in Word/VBA. It may be possible to add such characters in bookmark names in an XML format of the document, so if it is required, you can ask a separate question.

This feels like way too much code to post on SO. You really need to explain what framework you have in place and tell us where your hurdles are. We can't do this again. "Works for me". If you have any questions though don't hesitate to ask.

Run the "RunMe" macro at the bottom.

Private Function IsParagraphStyledWithHeading(para As Paragraph) As Boolean
    Dim flag As Boolean: flag = False
    If InStr(1, para.Style, "heading", vbTextCompare) > 0 Then
        flag = True
    End If
    IsParagraphStyledWithHeading = flag
End Function

Private Function GetTextRangeOfStyledParagraph(para As Paragraph) As String
    Dim textOfRange As String: textOfRange = para.Range.Text
    GetTextRangeOfStyledParagraph = textOfRange
End Function

Private Function BookmarkNameAlreadyExist(bookmarkName As String) As Boolean
    Dim bookmark As bookmark
    Dim flag As Boolean: flag = False
    For Each bookmark In ActiveDocument.Bookmarks
        If bookmarkName = bookmark.name Then
            flag = True
        End If
    Next
    BookmarkNameAlreadyExist = flag
End Function

Private Function CreateUniqueBookmarkName(bookmarkName As String)
    Dim uniqueBookmarkName As String
    Dim guid As String: guid = Mid$(CreateObject("Scriptlet.TypeLib").guid, 2, 36)
    guid = Replace(guid, "-", "", , , vbTextCompare)
    uniqueBookmarkName = bookmarkName & guid
    CreateUniqueBookmarkName = uniqueBookmarkName
End Function

Private Function BookmarkIt(rng As Range, bookmarkName As String)
    Dim cleanName As String: cleanName = MakeValidBMName(bookmarkName)
    If BookmarkNameAlreadyExist(cleanName) Then
        cleanName = CreateUniqueBookmarkName(cleanName)
    End If
    ActiveDocument.Bookmarks.Add name:=cleanName, Range:=rng
End Function

''shamelessly stolen from gmaxey at http://www.vbaexpress.com/forum/showthread.php?t=37674
Private Function MakeValidBMName(strIn As String)
    Dim pFirstChr As String
    Dim i As Long
    Dim tempStr As String
    strIn = Trim(strIn)
    pFirstChr = Left(strIn, 1)
    If Not pFirstChr Like "[A-Za-z]" Then
        strIn = "A_" & strIn
    End If
    For i = 1 To Len(strIn)
        Select Case Asc(Mid$(strIn, i, 1))
        Case 49 To 58, 65 To 90, 97 To 122
            tempStr = tempStr & Mid$(strIn, i, 1)
        Case Else
            tempStr = tempStr & "_"
        End Select
    Next i
    tempStr = Replace(tempStr, "  ", " ")
    MakeValidBMName = tempStr
End Function

Sub RunMe()
    Dim para As Paragraph
    Dim textOfPara As String
    For Each para In ActiveDocument.Paragraphs
        If IsParagraphStyledWithHeading(para) Then
            textOfPara = GetTextRangeOfStyledParagraph(para)
            If para.Range.Bookmarks.Count < 1 Then
                BookmarkIt para.Range, textOfPara
            End If
        End If
    Next
End Sub
JohnZaj
  • 3,080
  • 5
  • 37
  • 51