error using code second time

  • Thread starter Thread starter Kees
  • Start date Start date
K

Kees

I'm opening and edeting an existing Word doc. using the following code in
Access.
Every time using it the fist time it works ok, but using it the second time
(for a new document) I get an error. Only after a restart I can use the code
again, but using the second time again an error.
the error is: the external server computer does not exist or is not
available.
Some one any idee ??

Kees


Dim objWord As Object

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open FileName:=stDocName

Set myrange = ActiveDocument.Range(Start:=0, End:=0)
With myrange.Find
..ClearFormatting
..Text = "===CONCEPT==="
With .Replacement
..ClearFormatting
..Text = ""
End With
..Execute Replace:=wdReplaceAll, _
Format:=True, MatchCase:=False, _
MatchWholeWord:=True
End With

ActiveDocument.Close

objWord.Quit SaveChanges:=wdSaveChanges

Set objWord = Nothing
Set myrange = Nothing
'
 
Hi Kees,

I can see two problems in your code. First, you create a Word
Application object
Set objWord = CreateObject("Word.Application")

but then actually make you changes to an unqualified "ActiveDocument":
Set myrange = ActiveDocument.Range(Start:=0, End:=0) ....
ActiveDocument.Close

Normally one would expect
Set myrange = objWord.ActiveDocument.Range(...)

and
objWord.ActiveDocument.Close SaveChanges:=True

Without the SaveChanges parameter, the .Close will await confirmation
from the user about whether or not to save changes.
 
Back
Top