Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-10-2025, 01:59 AM
RRB's Avatar
RRB RRB is offline Macro for creating bookmarks Windows 11 Macro for creating bookmarks Office 2021
Susan Flamingo
Macro for creating bookmarks
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 302
RRB is on a distinguished road
Default Macro for creating bookmarks

Hi, Dear Friends!



This is way over my head and ChatGPT is choking on it also, so I will have to bother you friends for some help:

A macro that will:

1. search the doc for any text string formatted with the char style "myStyle".

2. Create a bookmark named as the the text string found (I guess the spaces will have to be removed. There will never be any special chars with that format only some Hebrew chars)

3. move till next instance and repeat till end of doc.

This will result in the making of around 800 BM. Will this make the file bloated and bogged down?

Thank you, and have a good day!
Susan Flamingo
Reply With Quote
  #2  
Old 07-10-2025, 03:50 PM
macropod's Avatar
macropod macropod is offline Macro for creating bookmarks Windows 10 Macro for creating bookmarks Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

For example:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, j As Long, StrBkMk As String
Dim StrNoChr As String: StrNoChr = """‘'’“”*!.,/\:;?|{}[]()&" & vbCr & Chr(11) & Chr(12) & vbTab
With ActiveDocument.Range
  For i = .Bookmarks.Count To 1 Step -1
    With .Bookmarks(i)
      If Left(.Name, 1) = "_" Then If Right(.Name, 3) Like "###" Then .Delete
    End With
  Next
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Style = "myStyle"
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
  End With
  Do While .Find.Execute
    StrBkMk = Replace(Trim(.Text), " ", "_")
    For j = 1 To Len(StrNoChr): StrBkMk = Replace(StrBkMk, Mid(StrNoChr, j, 1), ""): Next
    i = i + 1: .Bookmarks.Add "_" & Left(StrBkMk, 36) & Format(i, "000"): .Collapse wdCollapseEnd
    If .Information(wdWithInTable) = True Then
      If .End = .Cells(1).Range.End - 1 Then
        .End = .Cells(1).Range.End
        .Collapse wdCollapseEnd
        If .Information(wdAtEndOfRowMarker) = True Then
          .End = .End + 1
        End If
      End If
    End If
    .Collapse wdCollapseEnd
    If (ActiveDocument.Range.End - .End) < 2 Then Exit Do
  Loop
End With
Application.ScreenUpdating = True
MsgBox i & " bookmarks added."
End Sub
Do note that the inserted bookmarks are hidden and numbered. That's because:
• bookmarks cannot begin with numbers; and
• bookmark names must be unique. Numbering them obviates the potential for duplicate names
Making the bookmarks hidden prefixes them with an underscore, which avoids the first problem, whilst numbering avoids the second problem. Changing the prefix character to something else will make the bookmarks visible.

As coded, the macro will replace any of the existing bookmarks it has previously inserted on each run.

Given that you're using Hebrew, you might need to change:
Left(StrBkMk, 36)
to:
Right(StrBkMk, 36)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 07-10-2025, 04:36 PM
Guessed's Avatar
Guessed Guessed is offline Macro for creating bookmarks Windows 10 Macro for creating bookmarks Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Paul posted a good solution while I was playing with this. I did find a clean Regex method to make the bookmark name safe so I'll add my solution anyway. I didn't consider table cells and didn't do any testing with tables so Paul's solution may be a better fit for you.
Code:
Sub AddBookmarksMyStyle()
  Dim aRng As Range, sBk As String
  Set aRng = ActiveDocument.Range

  With aRng.Find
    .ClearFormatting
    .Style = ActiveDocument.Styles("myStyle")
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchKashida = False
    .MatchDiacritics = False
    .MatchAlefHamza = False
    .MatchControl = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    Do While .Execute = True
      sBk = MakeValidBookmarkName(aRng.Text)
      ActiveDocument.Bookmarks.Add Name:=sBk, Range:=aRng
      aRng.Collapse Direction:=wdCollapseEnd
    Loop
  End With
End Sub

'---------------------------------------------------------
Function MakeValidBookmarkName(ByVal inputName As String) As String
  Dim regex As Object
  Set regex = CreateObject("VBScript.RegExp")
  regex.Pattern = "[^a-zA-Z0-9_]"  ' Define pattern to match invalid characters (anything not alphanumeric or underscore)
  regex.Global = True
  MakeValidBookmarkName = regex.Replace(inputName, "_")  ' Replace invalid characters with underscore
  If IsNumeric(Left(MakeValidBookmarkName, 1)) Then  ' Solve if name starts with number
    MakeValidBookmarkName = "BM_" & MakeValidBookmarkName
  End If
End Function
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 07-11-2025, 05:40 PM
macropod's Avatar
macropod macropod is offline Macro for creating bookmarks Windows 10 Macro for creating bookmarks Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Andrew,

Two comments:
1. Your code will fail for strings of >40 characters (>37 if the first is numeric), since the maximum bookmark name length is 40 characters.
2. Also, depending on what's being bookmarked, your code won't leave all instances bookmarked if two or more ranges begin with the same 40 characters.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 07-13-2025, 04:07 AM
RRB's Avatar
RRB RRB is offline Macro for creating bookmarks Windows 11 Macro for creating bookmarks Office 2021
Susan Flamingo
Macro for creating bookmarks
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 302
RRB is on a distinguished road
Default

Thank you all very much for helping me with this.
Question: In Guessed's version I see it is locating the text string with the desired sytle but in Macropod's work I don't see that. Am I missing something?
again thank you
Reply With Quote
  #6  
Old 07-13-2025, 04:27 AM
macropod's Avatar
macropod macropod is offline Macro for creating bookmarks Windows 10 Macro for creating bookmarks Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Susan,

With Word's Find.Execute method, the found range automatically becomes the active range. And, when the Bookmarks.Add method is invoked without specifying the range to bookmark, it bookmarks the active range. I could have added .Duplicate as the optional Range parameter, but that would have been redundant.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 07-13-2025, 11:35 AM
RRB's Avatar
RRB RRB is offline Macro for creating bookmarks Windows 11 Macro for creating bookmarks Office 2021
Susan Flamingo
Macro for creating bookmarks
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 302
RRB is on a distinguished road
Default

Please excuse my ignorance, so you are saying it will only "grab" the text that is formatted with the style "MyStyle", even though it is not mentioned in the code?\

Your patients is appreciated!

SF

I just saw that your code does reference that style. Sorry, I messed that up
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro for creating bookmarks Creating a Macro in VBA where a Dropdown Box Populates Text Bookmarks below roseheller Word VBA 5 02-23-2021 03:10 PM
Macro to hide/unhide text to call variable bookmarks Dr. Z Word VBA 2 05-27-2017 08:20 PM
Macro for creating bookmarks Transfer word doc bookmarks to excel using macro Bedsy Word VBA 4 03-15-2015 10:58 PM
Macro for creating bookmarks Form updating Bookmarks - writes to the bookmarks multiple times PeterPlys Word VBA 13 01-14-2015 06:41 AM
Macro for creating bookmarks Macro to list all bookmarks Marrick13 Word VBA 4 02-09-2012 08:00 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:42 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft