#1
|
|||
|
|||
Remove text between brackets, bookmarks.
This code should remove text between [] inserted in bookmarks, the problem is that it's removing the 1st bookmark at the start of the document.
Any ideas on how to adjust this? Thanks. Code:
Sub Test() With ActiveDocument.Content.Find .Text = "\[*\]" .Replacement.Text = "" .ClearFormatting .Replacement.ClearFormatting .MatchWildcards = True .Execute Replace:=wdReplaceAll End With End Sub |
#2
|
||||
|
||||
You F/R expression cannot of itself remove a bookmark unless the [ and ] characters span the whole bookmarked range. Although bookmark boundaries look like the typed [ and ] characters, they cannot be accessed using those characters in a F/R expression.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
You may want to look at the section of this MVP page on working with bookmarks.
Word Macros and Visual Basic for Applications (VBA) FAQ |
#4
|
|||
|
|||
Quote:
To show where's each bookmark/name in a document, I use this code: Code:
Sub HighlightBookmarks() Dim objBookmark As Bookmark, objDoc As Document Application.ScreenUpdating = False Set objDoc = ActiveDocument With objDoc For Each objBookmark In .Bookmarks With objBookmark.Range .Text = "[ " & objBookmark.Name & " ]" .HighlightColorIndex = wdBrightGreen End With Next objBookmark End With Application.ScreenUpdating = True End Sub It is simple to reproduce the problem, insert a bookmark at the beginning of a document, type some text, insert another bookmark, run the macros, go to the list of bookmarks and see that the first bookmark is gone. Thanks |
#5
|
||||
|
||||
That is not the correct way to update a bookmark. If you use F5 and choose 'Bookmark' to goto one of your bookmarks, you'll see that the bookmark hasn't been expanded to span your range. The correct way has been discussed numerous times. See, for example: https://www.msofficeforums.com/word-...html#post67058. The same code can be used to both populate and clear the bookmarks.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Formatting text within brackets | jimmi6jjr | Word | 4 | 10-04-2022 09:33 PM |
find and delet all text within brackets and the brackets themselves | wrdy | Word | 2 | 08-03-2017 06:55 PM |
Word 2003 Brackets appear around copied text | barrage | Word | 1 | 12-16-2015 02:59 PM |
Microsoft Word macro to find text, select all text between brackets, and delete | helal1990 | Word VBA | 4 | 02-05-2015 03:52 PM |
Form updating Bookmarks - writes to the bookmarks multiple times | PeterPlys | Word VBA | 13 | 01-14-2015 06:41 AM |