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.
Since this is my first attempt at something like this, I'm sure there's something I've missed prior to closing the document after the first update.
Any pointers will be greatly appreciated.
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.
Since this is my first attempt at something like this, I'm sure there's something I've missed prior to closing the document after the first update.
Any pointers will be greatly appreciated.
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"
On Error GoTo GetNext
Set wdRange = WordDoc.GoTo(what:=wdGoToBookmark, Name:=Trim(updKey))
wdRange.Select
WordApp.Selection.TypeText Text:=updString
End If
GetNext:
.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
End With