none
Delete bookmark which doesn't have a name RRS feed

  • Question

  • Hi All,

    How can I delete a bookmark which doesn't have a name using VBA?

    When I use GUI to delete this bookmark then I'm able to do it. But when I use VBA code to delete it, word throws an error.

    Please guide me how to delete this bookmark.

    Thanks in advance

    Regards,
    Arul
    Thursday, April 18, 2013 9:16 AM

Answers

  • Hi Arul

    Thanks for the sample document. I can, indeed, see what you see and it's a new wrinkle on Word for me!

    I take it this client is working with some third-party software, then? And my guess would be that this software is generating the documents outside the Word application, which would be the only way this could happen. Or possibly some kind of document management / security system that's stripping out things, including bookmark names.

    In an ideal world, the client would go to the software manufacturer and get them to fix whatever tool they're using that's mangling Word's documents. Barring that...

    Next best would be to require them to send the documents in the post Word-2007 file format (*.docx). Because then you could process these files "painlessly" (I'll get into that).

    When I save as to *.docx, the following is the Word Open XML for the paragraph with the bookmark:

    <w:p><w:pPr><w:pStyle w:val="Heading4"/></w:pPr><w:bookmarkStart w:id="1" w:name=""/><w:bookmarkEnd w:id="1"/><w:r><w:t>This is place where the bookmark is inserted with no name</w:t></w:r></w:p>

    We can indeed see that the bookmark has no name. And if I type in a valid name, put the file back together and open it in Word, everything works as expected.

    So, if you can get this client to send you the files a *.docx - or if you could "save as" each file to that file format, possibly using Word Automation Services - you can write a little program in .NET to look for all bookmarks with no name and assign them names. Such a program runs extremely quickly as it works outside the Word application interface.

    If that sounds like a possibility to you, we can get into more detail...


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, April 19, 2013 8:55 AM
    Moderator

All replies

  • Why cant you give the bookmark a unique name, or a (unique) tag?

    Else you would probably need to loop through all bookmarks:

    Dim Number As Integer
        
        For Number = 1 To ActiveDocument.Bookmarks.Count
            If ActiveDocument.Bookmarks(Number).Range.Text = "Hello" Then
                ActiveDocument.Bookmarks(Number).Range.Delete
            End If
        Next


    • Edited by colaohye Thursday, April 18, 2013 10:20 AM
    Thursday, April 18, 2013 10:20 AM
  • Hi Arul,

    Hopefully this will help:

    Sub DeleteBookmarks()
        Dim i As Integer
        Dim doc As Word.Document
        Set doc = ActiveDocument
        ''example of deleting all bookmarks
        For i = doc.Bookmarks.Count To 1 Step -1
            doc.Bookmarks(i).Delete
        Next
        ''example for deleting a specific bookmark
        For i = doc.Bookmarks.Count To 1 Step -1
            If doc.Bookmarks(i).Name = "abc" Then doc.Bookmarks(i).Delete
        ''example for deleting a series of bookmarks
        ''assumes bookmark names begin with letters abc, for example abc001 or abc002
        For i = doc.Bookmarks.Count To 1 Step -1
            If Left(doc.Bookmarks(i).Name, 3) = "abc" Then doc.Bookmarks(i).Delete
        Next
        ''For best processing speed when deleting bookmarks use the Step -1 iteration technique
    End Sub
    


    Kind Regards, Rich ... http://greatcirclelearning.com

    Thursday, April 18, 2013 10:45 AM
  • Hi,

    Thanks for replies.

    I'm not the author of this document, so I have no idea how author has inserted bookmark without name.

    I have tried both codes, but still I'm getting the same error.

    "Run-time error '5891'"

    That property is not available on that object.

    http://social.msdn.microsoft.com/Forums/getfile/273579

    Any other trick?

    Regards,

    Arul

    Thursday, April 18, 2013 11:29 AM
  • http://social.msdn.microsoft.com/Forums/getfile/273579 - No item at that URL

    Show us your code. :)

    What kind of document are we talking about (file extensin; dotx, dotm, docm, doc....)?

    To set a Name for the bookmark, you can simply right-clcik the bookmark within Word, choose Properties, then there is a field which says "Bookmark", which will be equal to the Name. The Name attribute can be found at the bookmark, like so:

    ActiveDocument.Bookmarks(i).Name

    You do need to know something about the bookmark to be able to delete the correct bookmark. Is it the only bookmark in the document? Is it the very first? Does it have a default text? Does it have a tag? A name?

    Word version, 2007? 2010? 2013?



    • Edited by colaohye Thursday, April 18, 2013 1:16 PM
    Thursday, April 18, 2013 1:12 PM
  • Hi Arul

    The link you provide does not yield anything; it's an "unknown" address.

    Could you please upload a sample to skydrive, or something similar (that doesn't require anyone to log-in in order to access/download).

    Please also describe how you manage to delete these bookmarks through the UI.

    Are these by any chance unnamed FORM FIELDS?

    I've never heard of Word being able to create unnamed bookmarks, but form fields without a bookmark to identify them, definitely. All form fields in a document can be deleted using a technique like this:

    Sub DeleteFormFields
      Dim ffld as Word.FormField.
      For each ffld in ActiveDocument.FormFields
        ffld.Delete
      Next
    End Sub


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, April 18, 2013 1:17 PM
    Moderator
  • Hi All,

    Thanks for your replies,

    Sorry, for the link, I have tried to attach image using "insert image" button in reply editor. But before submitting it gives me an error saying body text should not have link or image, so i uploaded the image in specified URL. For me I'm still able to view image.

    Document extension is .doc (Word 97-2003)

    Normally when bookmarks are created word wont allow us to create a bookmark without a name, but I have no idea how the author of my document able to do it!

    To delete bookmark using GUI i used Insert > Bookmark, inside dialog box I can able to select bookmark which doesn't have name and deleted it.

    Even I record this whole event with "Record Macro". But when I ran this code, word is throwing me an error.

    Below are codes which I tried

    Sub deleteBkm()
    Dim i As Integer
    Dim bkmName As String
        With ActiveDocument.Bookmarks
            .ShowHidden = True
            For i = 1 To .Count
                bkmName = .Item(i).name
                ActiveDocument.Bookmarks(name).Delete
            Next i
        End With
    End Sub
    Sub DeleteBookmark()
    Dim i As Integer
    Dim doc As Word.Document
    Set doc = ActiveDocument
        
        For i = doc.Bookmarks.Count To 1 Step -1
            doc.Bookmarks(i).Range.Delete
        Next
    End Sub
    
    Sub DeleteBookmark()
    Dim bkm As Bookmark
    
        For Each bkm In ActiveDocument.Bookmarks
            bkm.Delete
        Next
    End Sub
    

    Below is the recorded macro using word

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    '
        ActiveDocument.Bookmarks("").Delete
        With ActiveDocument.Bookmarks
            .DefaultSorting = wdSortByName
            .ShowHidden = True
        End With
    End Sub

    As I said above, for all of these codes I get same error

    "Run-time error '5891'"

    That property is not available on that object.

    Thanks

    Regards,

    Arul

    Thursday, April 18, 2013 3:27 PM
  • Hi Arul

    I'm afraid there's not much we can do to help you if you can't provide us a sample document, as I mentioned in my previous reply.

    As far as I can tell, this document could be damaged. I suppose it's also possible that the bookmark names were given using characters the dialog box can't reproduce.

    About the only thing I can suggest would be to save such a document in the new *.docx file format. Then you can open it as a ZIP package and look at what is actually saved in the document structures. And correct it that way, too, if you'd like.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, April 19, 2013 5:58 AM
    Moderator
  • Hi,

    Thanks for your reply

    The problem with providing the sample document is that, the document we receive are law documents, which we are not supposed to provide it to outside world before the document is published.

    Anyway, I can copy the portion of the document and create a dummy sample document. But, how can I attached the document to this message board?

    As you suggested, if we are not able to find a solution delete the bookmark, either we have to convert it to rtf or docx and delete these kind of bookmarks and process it again. Problem with this procedure is that we are receiving 1000s of document per day, by this procedure it is going to slow down conversion process, that worries me a lot!

    Cheers,

    Arul

    Friday, April 19, 2013 6:59 AM
  • Hi Arul

    Yes, a portion, with meaningless text would be fine. Important is that we have a number of these odd bookmarks that you're only able to access through the dialog box.

    You can't attach files to forum discussions, but you can upload them to a service, such as skydrive (https://skydrive.live.com/) from which we can download them. Important from our standpoint is that we don't have to sign up/log into such a service.

    Which version of Word are YOU working with, by the way?

    And are these odd documents coming from any particular client? (Just yes/no at the moment, no details.)


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, April 19, 2013 7:32 AM
    Moderator
  • Hi,

    Here is the sky drive link https://skydrive.live.com/redir?resid=346683905F41BE56!120&authkey=!AN7oWfdFUGufK2U

    We are working with Office 2010.

    Yes, the files are coming from one client, but client also has no control over it.

    Cheers,
    Arul
    Friday, April 19, 2013 8:20 AM
  • Hi Arul

    Thanks for the sample document. I can, indeed, see what you see and it's a new wrinkle on Word for me!

    I take it this client is working with some third-party software, then? And my guess would be that this software is generating the documents outside the Word application, which would be the only way this could happen. Or possibly some kind of document management / security system that's stripping out things, including bookmark names.

    In an ideal world, the client would go to the software manufacturer and get them to fix whatever tool they're using that's mangling Word's documents. Barring that...

    Next best would be to require them to send the documents in the post Word-2007 file format (*.docx). Because then you could process these files "painlessly" (I'll get into that).

    When I save as to *.docx, the following is the Word Open XML for the paragraph with the bookmark:

    <w:p><w:pPr><w:pStyle w:val="Heading4"/></w:pPr><w:bookmarkStart w:id="1" w:name=""/><w:bookmarkEnd w:id="1"/><w:r><w:t>This is place where the bookmark is inserted with no name</w:t></w:r></w:p>

    We can indeed see that the bookmark has no name. And if I type in a valid name, put the file back together and open it in Word, everything works as expected.

    So, if you can get this client to send you the files a *.docx - or if you could "save as" each file to that file format, possibly using Word Automation Services - you can write a little program in .NET to look for all bookmarks with no name and assign them names. Such a program runs extremely quickly as it works outside the Word application interface.

    If that sounds like a possibility to you, we can get into more detail...


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, April 19, 2013 8:55 AM
    Moderator
  • Hi,

    Thanks for you reply.

    Word Automation Services is going to be a stand alone service within word or its going to be an service in SharePoint Server?

    Regards,

    Arul

    Friday, April 19, 2013 9:38 AM
  • Word Automation Services is part of Sharepoint 2010 (and 2013).

    Cindy Meister, VSTO/Word MVP, my blog

    Friday, April 19, 2013 9:58 AM
    Moderator
  • Hi,

    We don't have a SharePoint Server in our company! And, I don't know our company is going to invest on it right now.

    So, we will pre process our documents before going on to conversion stage.

    Thanks for your valuable replies.

    Cheers,

    Arul

    Friday, April 19, 2013 10:17 AM
  • For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, April 19, 2013 10:19 AM