![]() |
#1
|
||||
|
||||
![]()
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 |
#2
|
||||
|
||||
![]()
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 • 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] |
#3
|
||||
|
||||
![]()
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 |
#4
|
||||
|
||||
![]()
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] |
#5
|
||||
|
||||
![]()
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 |
#6
|
||||
|
||||
![]()
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] |
#7
|
||||
|
||||
![]()
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 |
![]() |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
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 |
![]() |
Bedsy | Word VBA | 4 | 03-15-2015 10:58 PM |
![]() |
PeterPlys | Word VBA | 13 | 01-14-2015 06:41 AM |
![]() |
Marrick13 | Word VBA | 4 | 02-09-2012 08:00 PM |