Home |
Search |
Today's Posts |
#1
|
|||
|
|||
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 |
#3
|
|||
|
|||
G'day Nick Curl Nick ,
Redefine the bookmark after overwriting it with your inserted text. Steve Hudson - Word Heretic steve from wordheretic.com (Email replies require payment) Without prejudice Nick Curl Nick reckoned: 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
|
|||
|
|||
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
|
|||
|
|||
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 | |
|
|
Similar Threads | ||||
Thread | Forum | |||
My computer freezes when opening a file in Office XP | Microsoft Word Help | |||
Opening A Text File With Word--Fixing Format | Microsoft Word Help | |||
Help opening a .dat Wordperfect file in Word | Microsoft Word Help | |||
File missing when loading by association | Microsoft Word Help | |||
When opening a file I want to see "file type", not showing now. | Microsoft Word Help |