Thread: bookmarks
View Single Post
  #5   Report Post  
Posted to microsoft.public.word.docmanagement
Lene Fredborg Lene Fredborg is offline
external usenet poster
 
Posts: 1,291
Default bookmarks

Zimmo,

Could it be that you want to delete not only the bookmarks but also the
bookmarked contents in the document?

Deleting a bookmark does not remove the bookmarked contents in the document
€“ the only visible thing that happens to the contents of the document is that
the bookmark brackets are removed (can only be seen if you turn on Tools
Options View tab Bookmarks).

Below you will find 2 macros €“ note that except from a single line of code,
the 2 macros are identical (macro 1 corresponds to the macro posted by Jay).
Both macros look for bookmarks with €œ_LINK€ in the name. The first macro
deletes only the bookmarks whereas the second deletes the bookmarked contents
in the document (which will also delete the bookmark in the same operation).

Macro 1:

Sub RemoveLINKBookmarks_KeepContents()
Dim bkm As Word.Bookmark

For Each bkm In ActiveDocument.Bookmarks
If InStr(bkm.Name, "_LINK") 0 Then
'Delete bookmark, keep contents
bkm.Delete
End If
Next bkm

End Sub

Macro 2:

Sub RemoveLINKBookmarks_DeleteContents()
Dim bkm As Word.Bookmark

For Each bkm In ActiveDocument.Bookmarks
If InStr(bkm.Name, "_LINK") 0 Then
'Delete the bookmark and the contents of the bookmark
bkm.Range.Text = ""
End If
Next bkm

End Sub

--
Regards
Lene Fredborg
DocTools €“ Denmark
www.thedoctools.com
Document automation €“ add-ins, macros and templates for Microsoft Word


"Zimmo" wrote:

I tried your code but it did not work either. Does the code
"LCase(bkm.Name)"in your string:
If InStr(LCase(bkm.Name), "_link") Then
bkm.Delete


refer to the name of the bookmark?

Here is my macro:

Sub Bookmarks()
'
' Bookmarks Macro

Dim bkm As Word.Bookmark
Dim doc As Word.Document
Set doc = ActiveDocument
For Each bkm In doc.Bookmarks
If InStr(LCase(bkm.Name), "_link") Then
bkm.Delete
End If
Next

End Sub


"Jay Freedman" wrote:

I suspect that the capitalization of the bookmark names isn't exactly like
"_LINK"; maybe it's "_Link" or something else. The InStr function does a
case-sensitive comparison. You can convert the names to all uppercase with
the UCase function, or to all lowercase with the LCase function, and supply
the coresponding string for comparison.

Besides that, your macro is unnecessarily complex; there's no need to store
the names in an array before deleting the bookmarks. This works for me:

Dim bkm As Word.Bookmark
Dim doc As Word.Document
Set doc = ActiveDocument
For Each bkm In doc.Bookmarks
If InStr(LCase(bkm.Name), "_link") Then
bkm.Delete
End If
Next

BTW, future questions about macros should be posted in one of the VBA
newsgroups, such as microsoft.public.word.vba.beginners or ...vba.general.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Zimmo wrote:
The code shown below was created in Visual Basic Editor and saved in
the normal.dot.. It appears in the Word document under MACROS but
when I try to run it in my document nothing happens. The bookmarks
are still there. Am I doing something wrong? I have close to 100
bookmarks that need to be removed.

Sub RemoveLINKBookmarks()
Dim doc As Word.Document
Dim bkm As Word.Bookmark
Dim aBKM() As String
Dim counter As Long

Set doc = ActiveDocument
counter = 0
ReDim aBKM(doc.Bookmarks.Count)
For Each bkm In doc.Bookmarks
If InStr(bkm.Name, "_LINK") 0 Then
aBKM(counter) = bkm.Name
counter = counter + 1
End If
Next
For counter = LBound(aBKM()) To UBound(aBKM())
If Len(aBKM(counter)) 0 Then
doc.Bookmarks(aBKM(counter)).Delete
End If
Next
End Sub