Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
bookmarks
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 |
#2
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
bookmarks
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 |
#3
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
bookmarks
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 |
#4
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
bookmarks
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? That's correct. bkm.Name is a string containing the name of the bookmark, such as _LINK1234. The LCase function changes any uppercase letters to lowercase, so the result of the expression LCase(bkm.Name) for that example would be _link1234. The InStr function then finds the position of the substring _link within that string, and the result should be the number 1, which is interpreted as True by the If statement. Instead of just running the macro from the Macros dialog and not seeing anything happen, try this: Open the VBA editor to the macro and press the F8 key. Each time you press F8, the next statement is executed and the macro pauses, as shown by highlighting in the code. If you place the mouse cursor on a variable name, such as bkm.Name, you'll see the variable's value at that moment. Alternatively, you can use the right-click menu to add variable names to the Watch window. Continue pressing F8 and checking variable values. When the bookmark being processed is one that you think should be deleted, watch the highlighting to see whether the line bkm.Delete is executed or, if not, why not. 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 That code looks right and, as I said, it worked in my trials. -- 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. |
#5
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
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 |
Reply |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
How to remove all bookmarks in 40 pg doc? | Microsoft Word Help | |||
bookmarks shown in doc | Microsoft Word Help | |||
Bookmarks, hyperlinks and 'save as' | Microsoft Word Help | |||
troubleshooting hyperlink bookmarks | Page Layout | |||
Bookmarks appearing in HTML Format document links | Microsoft Word Help |