Reply
 
Thread Tools Display Modes
  #1   Report Post  
Nick Curl
 
Posts: n/a
Default Loss of Bookmark After Re-opening File

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

  #2   Report Post  
Jay Freedman
 
Posts: n/a
Default

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 FAQ: http://word.mvps.org

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

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


  #4   Report Post  
Nick Curl
 
Posts: n/a
Default

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.

"Jay Freedman" wrote:

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 FAQ: http://word.mvps.org

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

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



  #5   Report Post  
Jay Freedman
 
Posts: n/a
Default

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!

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org

Nick Curl wrote:
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.

"Jay Freedman" wrote:

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 FAQ: http://word.mvps.org

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

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



Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
My computer freezes when opening a file in Office XP Sundance Microsoft Word Help 1 December 30th 04 06:14 AM
Opening A Text File With Word--Fixing Format KlikThis Microsoft Word Help 2 December 22nd 04 02:16 PM
Help opening a .dat Wordperfect file in Word Sunsetinaz Microsoft Word Help 3 December 3rd 04 12:40 AM
File missing when loading by association Bad Horsey Microsoft Word Help 1 November 30th 04 06:33 PM
When opening a file I want to see "file type", not showing now. John at Air, Land & Sea Microsoft Word Help 1 November 24th 04 03:09 PM


All times are GMT +1. The time now is 11:45 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"