Code only works every other time.

PortyAL

Craigavon, N.Ireland
Local time
Today, 00:13
Joined
Apr 16, 2007
Messages
24
Hi

I am trying to use the following code to open a Word template, insert text based on the field [Job] and then save the document to folder as per the field [docfolder].

Code:
 Sub msq()

Dim MSQname As String

MSQname = Forms![frm recs tracked jobs]![docfolder] & "\" & Forms![frm recs tracked jobs]![Job] & " MSQ.doc"

Set objword = New Word.Application

    With objword
      .Visible = True
      .Documents.Add Template:=("i:\ia manual\management satisfaction questionnaire.dot")
      .Selection.GoTo Name:=("JobName")
      .Selection.TypeText Text:=(Forms![frm recs tracked jobs]![Job])
      
    End With

Word.Application.ActiveDocument.SaveAs (MSQname)
Word.Application.Quit

End Sub

The code works perfectly but only on every 2nd attempt. When it doesn't work I get the following error:

Run time error '462". The remote server machine does not exist or is unavailable.

The error concerns the line Word.Application.ActiveDocument.SaveAs (MSQname)

Any advice would be greatly appreciated.

Thanks

AL
 
These two lines

Word.Application.ActiveDocument.SaveAs (MSQname)
Word.Application.Quit

are probably the root of the issue, as they might be instantiating a new instance of Word (check the Task Manager). Change them to work with your application object variable

bjword.ActiveDocument.SaveAs MSQname
objword.Quit
doevents
set objword=nothing

(though, I'm more fan of declaring and instamtiating a document object, too, but that's more preferences)
 

Users who are viewing this thread

Back
Top Bottom