Cannot find bookmark (error 5678)

Z

zSplash

Using code to add rows to a table, I then I want to return to the original
spot the sub was invoked. I have inserted a bookmark "StartHere", at
beginning of code. Then, at the end of the code (after adding many rows,
which are out of the window view), I try to re-position the "StartHere"
bookmark in the window, unsuccessfully. Here's my code to reposition:
ActiveDocument.ActiveWindow.ScrollIntoView Selection.Range
ActiveDocument.GoTo what:=wdGoToBookmark, Name:="StartHere"
ActiveDocument.Bookmarks("StartHere").Delete

The Bookmark exists, but I get a run-time error 5678 "Word cannot find the
requested bookmark". The start position remains out of view (if I comment
out the "goto" code). Can someone help?

TIA
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < zSplash > écrivait :
In this message, < zSplash > wrote:

|| Using code to add rows to a table, I then I want to return to the
original
|| spot the sub was invoked. I have inserted a bookmark "StartHere", at
|| beginning of code. Then, at the end of the code (after adding many rows,
|| which are out of the window view), I try to re-position the "StartHere"
|| bookmark in the window, unsuccessfully. Here's my code to reposition:
|| ActiveDocument.ActiveWindow.ScrollIntoView Selection.Range
|| ActiveDocument.GoTo what:=wdGoToBookmark, Name:="StartHere"
|| ActiveDocument.Bookmarks("StartHere").Delete
||
|| The Bookmark exists, but I get a run-time error 5678 "Word cannot find
the
|| requested bookmark". The start position remains out of view (if I
comment
|| out the "goto" code). Can someone help?

If all you want to do is put the cursor back where it was then try something
like:

'_______________________________________
Sub MySub()

Dim StartRange as Range

Set StartRange = Selection.Range

'Do your stuff here

StartRange.Select

End Sub
'_______________________________________

Also, learn about the Range object. If you use the Range object whenever you
can, most of the time you won't have to worry about the cursor... it'll stay
put because you will not be using the Selection object. OTOH, sometimes you
have no choice, like when dealing with page number information...

You can also use specific objects that will not involve the Selection
object. In your case, you are dealing with tables. You can add rows without
moving the cursor:

'_______________________________________
Dim MyTable As Table

Set MyTable = ActiveDocument.Tables(1)

With MyTable

.Rows.Add .Rows(3) 'Add row before third one
.Rows.Add 'Add row at the end

End With
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
P

Pete Bennett

This might be a bit of a silly question, but doesn't Application.GoBack do
the right thing for you?

It resets the insertion point to the last position before you started moving
around the document. You can use the command multiple times if neccessary...

Pete.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Pete Bennett > écrivait :
In this message, < Pete Bennett > wrote:

|| This might be a bit of a silly question, but doesn't Application.GoBack
do
|| the right thing for you?
||
|| It resets the insertion point to the last position before you started
moving
|| around the document. You can use the command multiple times if
neccessary...
||

Not really. Two problems with that method in this particular context:

1. If the user just moves the insertion point without actually performing an
action, Word does not mark that location as a GoBack point.
2. There is a limit of three "go back" points. If the macro performs more
than three actions using the selection object, the original user defined
starting location will not be reachable via GoBack (Which is the same as
SHIFT-F5).

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
P

Pete Bennett

Point taken Jean-Guy, but sometimes if an easy option works, it can save
quite a few lines of code in having to mess around with creating and then
deleting bookmarks (oh, and making sure the bookmark doesn't exist already
before creating a new one).

Pete.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Pete Bennett > écrivait :
In this message, < Pete Bennett > wrote:

|| Point taken Jean-Guy, but sometimes if an easy option works, it can save
|| quite a few lines of code in having to mess around with creating and then
|| deleting bookmarks (oh, and making sure the bookmark doesn't exist
already
|| before creating a new one).
||

Very true. We often tend to forget the age old K.I.S.S principle, me being
the first one to admit it!
But I did post an alternative that only required 3 lines of code.
OTOH, your suggestion may be workable for the OP, depending on his
particular context, about which he did not really elaborate!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
Z

zSplash

Thank you so much for your good help, Jean-Guy. Your code did the trick,
and I appreciate the good direction re: Ranges. I was unaware of how much
better it is to use vs. Select.

Thanks also to Peter for his suggestion. I have already used that idea.

st.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < zSplash > écrivait :
In this message, < zSplash > wrote:

|| Thank you so much for your good help, Jean-Guy. Your code did the trick,
|| and I appreciate the good direction re: Ranges. I was unaware of how
much
|| better it is to use vs. Select.

You're welcome :)

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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