Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code only works every other time ??? 1

Status
Not open for further replies.

PortyAL

Technical User
May 13, 2005
126
GB
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
 
How are you calling your sub?

Have you tried testing for the values of
Forms![frm recs tracked jobs]![docfolder]
&
Forms![frm recs tracked jobs]![Job]

I'm guessing one of these is probably blank and hence it's having a little freak out. Try declaring new variables and setting their value to the above. If still showing blank then try setting focus to each item.
 
You need to change this:
[tt]Word.Application.ActiveDocument.SaveAs (MSQname)
Word.Application.Quit[/tt]

To
[tt]objWord.ActiveDocument.SaveAs (MSQname)
objWord.Quit
Set objWord=Nothing[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top