A macro to eliminate bookmarks within a block of selected text?

D

DeeDeeCee

I cut-and-paste things from the documents, and sometimes they come with a
bunch of bookmarks I don't want cluttering up my receiving-document. I'm new
to VB, have managed to record a few macros, but can't write them myself. Has
anyone written a macro they can share which: 1) Goes to a highlighted area;
2) Identifies all the bookmarks; 3) Then deletes them. Much appreciated.

ddc
 
G

Gordon Bentley-Mix

It can certainly be done, but as a VBA "newbie" you might find other methods
easier. For example, if you click Edit | Paste Special... and select
"Unformatted text", the bookmarks are removed automatically. Alternatively,
if you are using Word 2003 (and possibly other versions as well), you should
be presented with a "Paste Options" tag after pasting with an option to "Keep
Text Only". This will also strip out the bookmarks.

If neither of these options appeal to you then post back and we'll see what
we can come up with.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
L

Lene Fredborg

....and if you still want a macro:

The following macro will delete all bookmarks in the selection, incl. hidden
bookmarks (such as _Ref bookmarks).


Sub DeleteBookmarksInSelection()
Dim oBookmark As Bookmark
Dim bShowHidden As Boolean

'Save status of "Show Hidden" bookmarks
bShowHidden = ActiveDocument.Bookmarks.ShowHidden

'Make sure to also delete hidden bookmarks
ActiveDocument.Bookmarks.ShowHidden = True

For Each oBookmark In Selection.Bookmarks
oBookmark.Delete
Next oBookmark

'Set "Show hidden" back to initial state
ActiveDocument.Bookmarks.ShowHidden = bShowHidden

End Sub


If you need help on installing a macro, see:
http://www.gmayor.com/installing_macro.htm


If you want to keep hidden bookmarks, delete the line:

ActiveDocument.Bookmarks.ShowHidden = True

In that case, you can also delete all lines in which bShowHidden is found.

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
J

Jonathan West

Lene Fredborg said:
...and if you still want a macro:

The following macro will delete all bookmarks in the selection, incl.
hidden
bookmarks (such as _Ref bookmarks).


Sub DeleteBookmarksInSelection()
Dim oBookmark As Bookmark
Dim bShowHidden As Boolean

'Save status of "Show Hidden" bookmarks
bShowHidden = ActiveDocument.Bookmarks.ShowHidden

'Make sure to also delete hidden bookmarks
ActiveDocument.Bookmarks.ShowHidden = True

For Each oBookmark In Selection.Bookmarks
oBookmark.Delete
Next oBookmark

'Set "Show hidden" back to initial state
ActiveDocument.Bookmarks.ShowHidden = bShowHidden

End Sub

Actually, that will delete precisely half of the bookmarks in the selection.
This will delete them all

Sub DeleteBookmarksInSelection()
Dim bShowHidden As Boolean
Dim n As Long

'Save status of "Show Hidden" bookmarks
bShowHidden = ActiveDocument.Bookmarks.ShowHidden

'Make sure to also delete hidden bookmarks
ActiveDocument.Bookmarks.ShowHidden = True

For n = 1 to Selection.Bookmarks.Count
Selection.Bookmarks(1).Delete
Next n

'Set "Show hidden" back to initial state
ActiveDocument.Bookmarks.ShowHidden = bShowHidden

End Sub
 
L

Lene Fredborg

Jonathan,

It is late here so I may be blind. I do not see any left over bookmarks
using my version of the macro.

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
D

DeeDeeCee

All the information was very useful, thank you. The paste-special will be a
good way to deal with the problem. But Lene's macro was easy to install and
worked well--I didn't see any undeleted bookmarks when I tested it. But if it
perhaps misses the hidden ones, I have Jonathan's handy for future reference.



Thanks.

dc
 
J

Jonathan West

Lene Fredborg said:
Jonathan,

It is late here so I may be blind. I do not see any left over bookmarks
using my version of the macro.

Curious. It used to be that cycling through the bookmarks in the way you
describe would miss every second bookmark, since by deleting the first, what
was previously the second is now the first, and by going on to the second
you would in fact go on to the third and miss the second.

But I've tried your macro and it does now work on Word 2003. Somebody at
Microsoft must have done a bug fix on this and I never heard about it.

In general, it is dangerous to assume that the indexing will be kept
straight when working through collections in Word and deleting individual
members of the collection.
 
L

Lene Fredborg

I don't think I have ever experienced problems with the method I used in the
macro – I did not refer to index numbers. I imagined you were thinking of the
following solution which would have deleted every second bookmark (deleting
bookmark n instead of bookmark 1):

For n = 1 to Selection.Bookmarks.Count
Selection.Bookmarks(n).Delete
Next n

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
J

Jonathan West

Lene Fredborg said:
I don't think I have ever experienced problems with the method I used in
the
macro - I did not refer to index numbers. I imagined you were thinking of
the
following solution which would have deleted every second bookmark
(deleting
bookmark n instead of bookmark 1):

For n = 1 to Selection.Bookmarks.Count
Selection.Bookmarks(n).Delete
Next n

No, it did definitely also apply to going through with a For Each loop as
well. I remember running tests on both ways of doing it. Somebody has been
doing some cleanup on the object model.

I've learned something today!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top