Hi all,
Got stuck on this one and can't find out whats wrong:
I have some code which opens a new Word document and then pastes some stuff in from the Db. Everything works OK the first time the code runs but the second time I get the error message "RPC Server is unavailable". Access has to be closed and reopened for it to work again.
Searching the arcives I found this article:
http://www.access-programmers.co.uk/forums/showthread.php?s=&threadid=34881
The final problem of Susan's seems similar but there is no answer.
Here's the code in question (I've chopped out various string handling bits to reduce its size).
---------------------------
Dim WordApp As Object
Dim StrARTemplate As String
Dim StrARDescription As String
Dim strAttachments As String
StrARTemplate = HomePath & "AR_Add_Template.dot"
On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WordApp = CreateObject("Word.Application")
End If
On Error GoTo 0
WordApp.Visible = True
WordApp.WindowState = wdWindowStateMaximize
WordApp.Documents.Add Template:=StrARTemplate, NewTemplate:=False
StrARDescription = "Test descriptive text"
StrAttachments = "Test text"
With WordApp.Selection
.Goto what:=wdGoToBookmark, Name:="description"
.TypeText StrARDescription
.Goto what:=wdGoToBookmark, Name:="attachments"
.TypeText strAttachments
End With
DoEvents
If Dir("c:\Div1\AR_Add.doc") <> "" Then Kill "C:\Div1\AR_Add.doc"
'=>Code on next line causes " RPC unavailable" error on second execution of code only
With WordApp.Documents(ActiveDocument.Name)
.SaveAs FileName:="c:\Div1\AR_Add", FileFormat:=wdWordDocument
End With
Set WordApp = Nothing
--------------------------------
The code is behind a button to generate the word doc from a record.
Any help as to why this all goes pear-shaped the second time around much appreciated.
Got stuck on this one and can't find out whats wrong:
I have some code which opens a new Word document and then pastes some stuff in from the Db. Everything works OK the first time the code runs but the second time I get the error message "RPC Server is unavailable". Access has to be closed and reopened for it to work again.
Searching the arcives I found this article:
http://www.access-programmers.co.uk/forums/showthread.php?s=&threadid=34881
The final problem of Susan's seems similar but there is no answer.
Here's the code in question (I've chopped out various string handling bits to reduce its size).
---------------------------
Dim WordApp As Object
Dim StrARTemplate As String
Dim StrARDescription As String
Dim strAttachments As String
StrARTemplate = HomePath & "AR_Add_Template.dot"
On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WordApp = CreateObject("Word.Application")
End If
On Error GoTo 0
WordApp.Visible = True
WordApp.WindowState = wdWindowStateMaximize
WordApp.Documents.Add Template:=StrARTemplate, NewTemplate:=False
StrARDescription = "Test descriptive text"
StrAttachments = "Test text"
With WordApp.Selection
.Goto what:=wdGoToBookmark, Name:="description"
.TypeText StrARDescription
.Goto what:=wdGoToBookmark, Name:="attachments"
.TypeText strAttachments
End With
DoEvents
If Dir("c:\Div1\AR_Add.doc") <> "" Then Kill "C:\Div1\AR_Add.doc"
'=>Code on next line causes " RPC unavailable" error on second execution of code only
With WordApp.Documents(ActiveDocument.Name)
.SaveAs FileName:="c:\Div1\AR_Add", FileFormat:=wdWordDocument
End With
Set WordApp = Nothing
--------------------------------
The code is behind a button to generate the word doc from a record.
Any help as to why this all goes pear-shaped the second time around much appreciated.