Consulting

Results 1 to 8 of 8

Thread: Solved: to delete what starts with...

  1. #1

    Solved: to delete what starts with...

    Hi everybody!

    Thanks for your great efforts here!

    I am new to VBA (although not for programming!!!) and I will highly appreciate your help.

    As for the code:

    [VBA]
    ActiveDocument.Bookmarks("BookmarkName").Delete
    [/VBA]

    specifying the BookmarkName, it is ok.

    But what I want to do is to delete bookmarks starting with specific set of characters, e.g. "aaa*".

    I have tried several things, but nothing worked well. Could you please help me with this?

    Thanks in advance,
    Mina

  2. #2
    Good Evening.

    Try:

    [VBA]
    Dim bmkCurrent as Bookmark

    For Each bmkCurrent in ActiveDocument.Bookmarks
    If Left(bmkCurrent.Name, 3) = "aaa" Then
    bmkCurrent.Delete
    End If
    Next bmkCurrent
    [/VBA]

    Scott

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi Mina,
    This works for me in Word 2003

    I attached a file too for you to check it out with. Run the sub delBkmks

    [vba]
    Option Explicit
    Sub delBkmks()
    Dim objBookmark As Bookmark
    For Each objBookmark In ActiveDocument.Bookmarks()
    If objBookmark.Name <> _
    Right(objBookmark.Name, Len("aaa") - 3) Then
    objBookmark.Range.Delete
    End If
    Next
    End Sub

    [/vba]

    Note: This method also deletes the range of the bookmark. You didn't specify but it's probably relevant. Easy to alter if that's not what you need.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I'm 3 minutes late and used the opposite approach.....it's good to have options.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5

    Options

    Hey Lucas.

    You're absolutely right, it's good to have options.

    The one difference I would like to note; however, is that your code deletes the Bookmark plus the text that's within that Bookmark's Range. Mine deletes only the Bookmark. That's a key difference depending on what Mina needs accomplished.

    Thanks for helping on the Forum.
    Scott

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi Scott, I had already thought of that and I added a line to my post to point that out. Sometimes it's hard to tell what folks want if they aren't specific.

    Thanks for your contributions here also Scott, folks like you are what help make this a great place to exchange ideas.

    Welcome to the forum.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    Dear Scott and Lucas,

    First of all, I like the wonderful spirit you show.

    Acutally, I need to delete bookmarks only. So Scott's approach works well for me.

    Many thanks Scott and Lucas,
    Mina

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi Mina, glad you got the help you needed. Be sure to mark your thread solved using the thread tools at the top of the page.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •