Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB app / Word bookmark problem 1

Status
Not open for further replies.

GhostWolf

Programmer
Jun 27, 2003
290
US
Reposted from VB5/6 forum:

I've created a VB app to open a Word document and update it, (using bookmarks), with data retrieved from an As/400. (Not all bookmarks are updated during execution.)

It works fine on the first run, but attempting to open/update the document a second time gives "Run-time error 5102: You entered multiple destinations for a page, line, footnote, endnote or comment" when the program attempts to point to the first bookmark for update.

<EDIT> It's beginning to look like the steps this program takes effectively remove the bookmarks, (the bookmarks the program updated are no longer in the document after the first execution). </EDIT>

Any pointers will be greatly appreciated.

NOTE: Following code is inside a With loop:

Code:
Set WordApp = CreateObject("Word.Application")
Set WordDoc = WordApp.Documents.Open(App.Path + "\ElOff.doc")
While Not .EOF
    updKey = Trim(!ELKEY)
    'Build update string            
    Set wdRange = WordDoc.GoTo(what:=wdGoToBookmark, Name:=updKey)    ' <<<<< ERROR generated here <<<<<
    wdRange.Select
    WordApp.Selection.TypeText Text:=updString
            
    If Not InList(Left(updKey, 2)) Then
' Build second update string
        updKey = Trim(updKey) + "e"

        Set wdRange = WordDoc.GoTo(what:=wdGoToBookmark, Name:=Trim(updKey))
        wdRange.Select
        WordApp.Selection.TypeText Text:=updString
    End If

    .MoveNext
Wend
.Close

updKey = "UpdDate"
updString = "Updated: " + Format(Now, "mm-dd-yyyy")
Set wdRange = WordDoc.GoTo(what:=wdGoToBookmark, Name:=Trim(updKey))
wdRange.Select
WordApp.Selection.TypeText Text:=updString

Set wdRange = Nothing
WordDoc.Save
WordDoc.Close
 
I guess you have to recreate the bookmarks, ie replace this:
Set wdRange = WordDoc.GoTo(what:=wdGoToBookmark, Name:=updKey) ' <<<<< ERROR generated here <<<<<
wdRange.Select
WordApp.Selection.TypeText Text:=updString
with this:
WordDoc.Bookmarks(updKey).Select
WordApp.Selection.Text = updString
WordDoc.Bookmarks.Add Name:=updKey, Range:=WordApp.Selection.Range

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

PH, looks like I get to join the multitudes you've helped. Your code has my program doing exactly what I expected of it. Thank you.
.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top