Am getting unusual error when trying to open and save word documents from excel vba.
The following code works fine on the first run. Opens doc, saves as a new file, with values placed in bookmarks.
The problem is that even if I delete the saved file, i.e. 1234.doc, I receive the following error when I next try to run the macro (that is, if i comment the error handling out!) a second time.
Any assistance appreciated.
error msg :"Run-time error '-2147023174(800706ba)'
Automation Error
The RPC server is unavailable"
code;
Public Sub test2()
On Error GoTo errorHandler
Dim myDoc As Word.Document
Set wdApp = New Word.Application
With wdApp
.Visible = True
.WindowState = wdWindowStateMaximize
End With
wdApp.Documents.Open FileName:="d:\Abc.doc", ReadOnly:=True
Set myDoc = wdApp.ActiveDocument
myDoc.Bookmarks("test1").Range = "11111"
myDoc.Bookmarks("test2").Range = "2222"
ActiveDocument.SaveAs FileName:=("d:\1123.doc")
errorHandler:
Set wdApp = Nothing
Set myDoc = Nothing
Set mywdRange = Nothing
End Sub
The following code works fine on the first run. Opens doc, saves as a new file, with values placed in bookmarks.
The problem is that even if I delete the saved file, i.e. 1234.doc, I receive the following error when I next try to run the macro (that is, if i comment the error handling out!) a second time.
Any assistance appreciated.
error msg :"Run-time error '-2147023174(800706ba)'
Automation Error
The RPC server is unavailable"
code;
Public Sub test2()
On Error GoTo errorHandler
Dim myDoc As Word.Document
Set wdApp = New Word.Application
With wdApp
.Visible = True
.WindowState = wdWindowStateMaximize
End With
wdApp.Documents.Open FileName:="d:\Abc.doc", ReadOnly:=True
Set myDoc = wdApp.ActiveDocument
myDoc.Bookmarks("test1").Range = "11111"
myDoc.Bookmarks("test2").Range = "2222"
ActiveDocument.SaveAs FileName:=("d:\1123.doc")
errorHandler:
Set wdApp = Nothing
Set myDoc = Nothing
Set mywdRange = Nothing
End Sub