delete bookmark text

J

Joanne

I have several bookmarks on my docs, which I need to set to 'nothing'
after the job is finished printing.

I have tried

If oDocName.Bookmarks.Exists("FName") = True Then
.oDocName.Bookmarks.FName.Range.Text = ""
End If
If oDocName.Bookmarks.Exists("Midinit") = True Then
.oDocName.Bookmarks.Midinit.Range.Text = ""
End If
But it does not delete the bookmark values

I have also tried this code in my routine - it was supplied by someone
on these newsgroups, but it also does not do the job

For Each bkm In oDocName.Bookmarks
bkm.Range.Text = ""
Next

Can anyone give me a clue what is wrong here?
Thank you
Joanne
 
D

Doug Robbins

Either the bookmark no longer exists, or the text that you want to delete
may not be in the bookmark. Turn on the display of bookmarks under
Tools>Options>View and you can see it the above is the case.

See the working with bookmarks item on the Word MVP website at:

http://word.mvps.org/FAQs/MacrosVBA/WorkWithBookmarks.htm


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
J

Joanne

Doug
Thanks for your input. I read the articles at the mvp site and am now
wondering if my problem is using the wrong kind of bookmark. I am
using the type with the I Beam, not the 'selected text' type.

Below is the code I am trying to delete them with (instead of the
other routine that I was trying when I posted originally)

If Not oRst Is Nothing Then
Do While Not oRst.EOF
sFilename = "" & oRst("DocNamePath")
If Len(Dir$(sFilename)) > 0 Then
Set oDocName = oWordapp.Documents.Open(sFilename)
If Not oDocName Is Nothing Then
For Each bkm In oDocName.Bookmarks
Debug.Print bkm
bkm.Range.Text = ""
Next
oDocName.Save
DoEvents
oDocName.Close
oRst.MoveNext

When I look in the immediate window, the names of each bookmark in the
document are listed, So I know the doc is loaded (anyway the doc name
shows itself when I hover the mouse over the variable), the bookmark
collection is recognized, but still none of the text values in the
bookmark are being deleted. Can't figure it out, probably some small
thing I am missing.
Perhaps you can spot it?
Thanks for your expertise
Joanne
 
J

Joanne

Doug
Forgot to mention that I have checked the doc, and all the bookmarks
are on the doc, and the routine that sets the text in the bookmarks
runs fine, all text entries are present in the doc. The problem now
is to get the buggers out of there when I'm done processing!!
 
J

Joanne

Doug
In a further attempt to see what the problem is, I set a breakpoint on
this line: Do While Not oRst.EOF
Then using F8 and viewing the doc and the immediate window and the
code as I step thru the code, I can see that the correct doc is open,
the bookmarks are visible, as is their text entries, and I run thru
the for/next statement to grab each bookmark in turn (they are
printing in the immediate window as expected) and as a test I added a
space between the quote marks in this line:
bkm.Range.Text = " "
Well, the code went to each bookmark and added a space before the text
in the bookmark while leaving the text in tact but pushed to the right
one empty space, so I know they are all being recognized and I can
even edit them, but they will not delete when I use the line with no
spaces between the quote marks, like this: bkm.Range.Text = "".
I am completely baffeled here.

This is the entire routine code
Function EmptyBookmarks()
Dim oWordapp As Object
Dim oDocName As Object
Dim oRst As DAO.Recordset
Dim BsSql As String 'sql stmt to get docs with bookmarks
Dim sFilename As String
Dim bkm As Word.Bookmark

Set oWordapp = CreateObject("Word.Application")
oWordapp.Visible = True

BsSql = "SELECT tblDocumentList.DocNamePath FROM tblDocumentList" & _
" WHERE (((tblDocumentList.Bookmarks) = True))"

Set oRst = CurrentDb.OpenRecordset(BsSql)
If Not oRst Is Nothing Then
Do While Not oRst.EOF
sFilename = "" & oRst("DocNamePath")
If Len(Dir$(sFilename)) > 0 Then
Set oDocName = oWordapp.Documents.Open(sFilename)
If Not oDocName Is Nothing Then
For Each bkm In oDocName.Bookmarks
Debug.Print bkm
bkm.Range.Text = " "
Next
oDocName.Save
DoEvents
oRst.MoveNext
End If
End If
Loop
End If
oWordapp.Quit
oRst.Close
Set oWordapp = Nothing
Set oDocName = Nothing
Set oRst = Nothing
End Function
 
D

Doug Robbins

To delete the "bookmark text", in would have to be enclosed within the
bookmarks like:

[text to be deleted]

If as you say, you bookmarks look like an "I" beam

|text to be deleted

the text will not be deleted because it is not in the .Range of the
bookmark.

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
J

Jay Freedman

Hi Joanne,

This description, together with your earlier posting saying that your
bookmarks are I-beams, explains what's happening.

There is really only one kind of bookmark. If its start and end are
not in the same place, it shows on screen as a pair of brackets, [ ],
with the text between them. If the start and end *are* in the same
place, so there is no text inside, then the brackets get squashed
together so the verticals coincide and the arms stick out on opposite
sides, making the I-beam.

The VBA expression bkm.Range refers to the stretch of document between
the start and end of the bookmark. If they're in the same place, the
length of the range is 0. When you assign "nothing" text to that range
with the expression bkm.Range.Text = "", the result is *exactly* the
same as what you started with -- a bookmark with nothing inside! When
you assign a space to it, it opens up to be one character long, and
pushes all the following text to the right. You can't remove any
existing text this way.

If you want to remove some text by assigning "nothing" to
bkm.Range.Text, then bkm must start by enclosing the text you want to
remove. Just being a single point at the beginning of that text isn't
good enough.

If the bookmarks are single-point I-beams because some macro assigned
them that way, then that macro is doing it wrong. See
http://word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm for
the right way. If it's because you manually inserted the bookmarks
that way, then you have to select the text -- not just drop the
insertion point in front of the text -- before assigning a bookmark
name.
 
J

Joanne

Jay
Thank you so much for your great explanation of what was going on. I
got the information from the site you marked below (I had been sent to
that info before and I had read it, but without your explanation I
didn't catch what was important there ;(

Anyway, once I was able to explain about the I beam and the space so
you mvps could know what I was doing, you gave me a good answer to my
problem that I could understand (I hope to advance from the ranks of
'newbie' some day).

I used the info from the link below and am now able to do exactly what
I wanted to do.

Thanks to all of you for all the help you have given me to get this
app ready to go.

I have learned a great deal from all your patient help.
Thank you all very much
Joanne
 

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