Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2007

Tip: Looking for answers? Try searching our database.

Delete bookmark if it contains specific words

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
aehan - 12 Nov 2007 15:50 GMT
Hello all

I have some code which is supposed to delete data if it contains specific
words.  The code I have is as follows:

       If ActiveDocument.Bookmarks("test").Range.Words(2) = "Central
Service" Then
           With Selection
               .GoTo What:=wdGoToBookmark, Name:="test"
               .Delete
               .GoTo What:=wdGoToBookmark, Name:="test2"
               .MoveRight unit:=wdWord, Count:=4, Extend:=wdExtend
               .Delete
           End With
       End If

I have stepped through the code and it fails at the IF clause, so doesn't
run.  If, however, I only had one word in the bookmark and amended the if to
read:

       If ActiveDocument.Bookmarks("test").Range.Words(1) = "Central" Then
        With Selection
               .GoTo What:=wdGoToBookmark, Name:="test"
               .Delete
               .GoTo What:=wdGoToBookmark, Name:="test2"
               .MoveRight unit:=wdWord, Count:=4, Extend:=wdExtend
               .Delete
           End With
       End If

the code runs beautifully.  I really need to use the two word option, and I
have spent ages trying to work out why it doesn't work when looking for two
words.  Can anyone help me and tell me what I've done wrong please?

Thanks in frustration
Aehan
Jay Freedman - 12 Nov 2007 18:55 GMT
The expression .Words(2) means specifically "the word that is in the
second position in the bookmark's range". It cannot refer to two
words. If indeed "Central" is the second word in the range, then
"Service" could be the third word, or .Words(3).

I think the code you showed (which is obviously slightly modified from
macro recorder output) is far too specific about what it expects to
find in the bookmark, while at the same time being very inefficient.
If you just want to know whether the words "Central Service" are
_somewhere_ within the bookmark, and if they are then delete the whole
bookmark as well as the bookmark "test2", then all you need is this:

With ActiveDocument.Bookmarks("test").Range
  If InStr(.Text, "Central Service") Then
     .Delete
     ActiveDocument.Bookmarks("test2").Range.Delete
  End If
End With

This code doesn't have to move the cursor in the document.

>Hello all
>
[quoted text clipped - 32 lines]
>Thanks in frustration
>Aehan

--
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.
aehan - 12 Nov 2007 19:38 GMT
Hi Jay

Thank you very much, I've spent a whole afternoon and into this evening
trying to find a way of making this work.  As you guessed, I'm not too good
at this and tend to use the recorder, but I'm learning.  I really appreciate
your help.

Thanks
Aehan

> The expression .Words(2) means specifically "the word that is in the
> second position in the bookmark's range". It cannot refer to two
[quoted text clipped - 59 lines]
> Microsoft Word MVP        FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.