Loss of Bookmark After Re-opening File

G

Guest

I have a Word doc with bookmarks and a VB app that opens and closes the doc.
With the doc open, I can use the cmdopen sub (below) to update a bookmark's
text. The text changes and bookmark remains intact (braces are displayed). I
close the doc with cmdclose sub (below) and then execute cmdopen. The doc
opens showing bookmarks and changes intact. When I update the bookmark this
time, the text changes but the bookmark is destroyed (braces removed). Any
idea why the second opening destroys the bookmark when it's text is updated?

I have a VB app with the following code:

Dim wdApp As Word.Application
Dim doc As Word.Document

Private Sub cmdopen_Click()

Dim req As String
Dim rngRange As Word.Range

Set wdApp = New Word.Application
wdApp.Visible = True
Set doc = wdApp.Documents.Open("c:/mystuff/reqman/test.doc")

req = InputBox("enter req")
Set rngRange = doc.Bookmarks(req).Range

With rngRange
.Text = "test"
.Select
End With
doc.Bookmarks.Add req, Selection.Range
Set rngRange = Nothing

End Sub

Private Sub cmdclose_Click()

doc.Close
wdApp.Quit
Set doc = Nothing
Set wdApp = Nothing

End Sub
 
J

Jay Freedman

Hi Nick,

I'm not sure this will cure the problem, but try doing it without
involving the Selection at all:

Set rngRange = doc.Bookmarks(req).Range
rngRange.Text = "test"
doc.Bookmarks.Add req, rngRange
Set rngRange = Nothing

Is there anything special about the bookmark's environment? Is it
inside a table cell, or associated with a field?

--
Regards,
Jay Freedman
Microsoft Word MVP

On Mon, 3 Jan 2005 15:25:04 -0800, Nick Curl <Nick
 
W

Word Heretic

G'day Nick Curl <Nick (e-mail address removed)>,

Redefine the bookmark after overwriting it with your inserted text.


Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice
 
G

Guest

Jay,
That is exactly what I wound up doing and it worked for updating my bookmark.

According to MSDN, there can be only one Selection object at a time. When my
VB app uses the range.select method, text is highlighted. I think that this
Selection object is somehow getting "orphanned" when my app closes the doc.
When my VB app opens a new Word doc I think there is a conflict with the
Selection object. My VB app code simply ignores commands with Selection in
them (eg, msgbox) or I get a "remote server machine does not exist or is not
available" failure when trying to execute a Selection command.

Do you know how to de-select text in a doc from a VB app. I can't find
anything in the literature and my attempts to use Selection methods have
failed to de-select the text.
 
J

Jay Freedman

Hi Nick,

Just as there can't be more than one Selection at a time, there is *always*
one Selection -- it represents the "insertion point". It may be extended
over some text or it may be just a single point, but it's always there.

To make sure it isn't extended, use the Selection.Collapse method. You can
choose whether to collapse it to its starting point or its ending point by
supplying a parameter. In VBA the parameter values are named constants,
wdCollapseStart and wdCollapseEnd; the values of the constants are 1 and 0,
respectively. (If it's already collapsed, it doesn't change.)

If your VB app opens multiple documents but not a new instance of Word, the
one-and-only Selection (which is a property of the Application object) is in
the currently active document. Depending on how you handle things in VB,
Word could indeed get confused. Another reason to avoid the stinker whenever
possible!
 

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