Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-06-2021, 04:17 AM
eduzs eduzs is offline Remove text between brackets, bookmarks. Windows 10 Remove text between brackets, bookmarks. Office 2019
Expert
Remove text between brackets, bookmarks.
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 01-06-2021, 04:40 AM
macropod's Avatar
macropod macropod is offline Remove text between brackets, bookmarks. Windows 10 Remove text between brackets, bookmarks. Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

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]
Reply With Quote
  #3  
Old 01-06-2021, 07:03 AM
Charles Kenyon Charles Kenyon is offline Remove text between brackets, bookmarks. Windows 10 Remove text between brackets, bookmarks. Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,081
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

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
Reply With Quote
  #4  
Old 01-06-2021, 07:18 AM
eduzs eduzs is offline Remove text between brackets, bookmarks. Windows 10 Remove text between brackets, bookmarks. Office 2019
Expert
Remove text between brackets, bookmarks.
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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.
Hi macropod!

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
After that, I want to "undo" this action, so I use the above code (first), but this result in first bookmark at top of the document to be also deleted.

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
Reply With Quote
  #5  
Old 01-06-2021, 01:24 PM
macropod's Avatar
macropod macropod is offline Remove text between brackets, bookmarks. Windows 10 Remove text between brackets, bookmarks. Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

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]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove text between brackets, bookmarks. Formatting text within brackets jimmi6jjr Word 4 10-04-2022 09:33 PM
Remove text between brackets, bookmarks. find and delet all text within brackets and the brackets themselves wrdy Word 2 08-03-2017 06:55 PM
Remove text between brackets, bookmarks. 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
Remove text between brackets, bookmarks. Form updating Bookmarks - writes to the bookmarks multiple times PeterPlys Word VBA 13 01-14-2015 06:41 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:52 AM.


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