Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
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,185
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
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 11:04 PM.


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