Results 1 to 7 of 7
  1. #1
    Lounger
    Join Date
    Dec 2011
    Posts
    30
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Run through bookmarks, search for text, delete bookmark

    Hi everyone,

    I'm trying to come up with a way I can delete bookmarked text if it contains a particular string at the beginning.

    The background to this issue is that I'm creating some precedents for work, and trying to find a workaround for the problem where Word doesn't format outline numbered paragraphs correctly if they're part of a result of an IF field. So, I've decided that what I'll do instead is have an IF before the chunks of text that come in/out depending on the IF field. Like so: {IF {DocVariable varTest} = "True" "" "[DELETE DeleteIfFalse]"}.

    Then, I bookmark the chunk of text (including the IF field) and call it 'DeleteIfFalse' (or whatever).

    So, I then need to create a macro that, after the variables have been pulled in from the userform, will search the document for any bookmarked text, check if it begins with the "[DELETE" string, and if it does, then delete the bookmarked text.

    I have this so far, but it's just not working:

    Code:
    Dim oBook As Bookmark
    Dim StartWord As String
    
    StartWord = "[DELETE"
    
        For Each oBook In ActiveDocument.Bookmarks
        'if Range begins with [DELETE* then delete the bookmark
        If (oBook.Range.Text = StartWord & "*") Then
            oBook.Delete
            End If
        
        Next
        
    Set oBook = Nothing
    I don't get any error messages, but it just doesn't delete the bookmarked text that begins with that string. Any ideas?

    Thank you!

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Try:
    Code:
    Sub Demo()
    Dim BkMk As Bookmark
    For Each BkMk In ActiveDocument.Bookmarks
      With BkMk.Range
        MsgBox .Text
        If InStr(.Text, "[DELETE") > 0 Then .Delete
      End With
    Next
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. The Following User Says Thank You to macropod For This Useful Post:

    caracolesa (2014-11-02)

  4. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    You might also need to loop from the last bookmark to the first. Deleting a member of the collection working forward means the next item gets skipped
    Dim i as integer
    For i = ActiveDocument.Bookmarks.count to 1 step -1
    'your code in here using ActiveDocument.Bookmarks(i)
    Next i
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by Andrew Lockton View Post
    You might also need to loop from the last bookmark to the first. Deleting a member of the collection working forward means the next item gets skipped
    While that's often true, in this case it's not the bookmark that's being deleted but the range it refers to.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  6. #5
    Lounger
    Join Date
    Dec 2011
    Posts
    30
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks so much Paul! That does exactly what it's supposed to do.

    Now I'll just have to work out how to modify it so it doesn't need the msg box first (some of our documents will have 20 or 30 of these paragraphs, so the msg box will get quite irritating).

    Thank you!

  7. #6
    Lounger
    Join Date
    Dec 2011
    Posts
    30
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Just as an update, I used the SendKeys event to automatically click OK, which I know is problematic, but it works in this instance, so all solved!

    Thank you!

  8. #7
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    You could just delete the 'MsgBox .Text' from the code ... It was there just to show what is bookmarked
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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