delete text in enclosed bookmarks

A

arne

My code deletes the enclosed text [enclosed bookmarktext] and the
bookmarkname, also in header and footer and let
other NOT enclosed bookmarks live. That i the meaning.

Problem: it stops in the footer if I have an enclosed bookmark in the
header., and I want it to
end when finished at the beginning of the document,; Selection.HomeKey
Unit:=wdStory

What have I missed here?


Sub deleteenclosedbookmarks()
' On Error Resume Next
Dim allbookmks As Bookmark
For Each allbookmks In ActiveDocument.Bookmarks
If Len(allbookmks .Range) > 0 Then
allbookmks .Range.Text = Delete
Else
End If
Next allbookmks
End Sub
 
K

Klaus Linke

Hi Arne,

Your code shouldn't change the selection at all. What version of Word do you
use?

If the code does change the selection, it would look like a bug. A
work-around might be to remember the selection at the beginning of your
macro:

Dim rngOld as Range
Set rngOld = Selection.Range

and reset it at the end of the macro:
rngOld.Select

BTW,
allbookmks.Range.Text = Delete
sets the text to a variable with name "Delete".
Since you haven't defined that variable, it's defined as a Variant at that
point.
Since a string is expected for .Text, Word then uses an empty string.

A better way to code that would be
allbookmks.Range.Delete
or
allbookmks.Range.Text = ""

Better use "Option Explicit" to avoid problems with undeclared variables.

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top