Opening Word files from Access

F

free.teranews.com

I'm absolutely stuck.
The following code works perfectly the first time through, but when run
again fails with an error 462 (the remote server machine does not exist or
is unavailable). Simply resetting from the error and running again and it
works fine for one time only. There is no problem if you close the file,
open it again and close it again.
Closing the Form makes no difference, only closing the application puts
things back right
I've tried similar code calling an Excel file with no problems at all.
Help would be much appreciated.
Geoff

Public Sub Combo4_Click()
Dim objword As Word.Application

On Error Resume Next
Set objword = GetObject(, "Word.Application")
If objword Is Nothing Then
Set objword = New Word.Application
End If
On Error GoTo 0

objword.Visible = True
Documents.Open ("C:\windows\application data\microsoft\templates\u3a.dot")
Documents.Close
Set objword = Nothing
Word.Application.Quit

End Sub
 
D

Dave Jones

Geoff,

Think your problem lies with:
On Error GoTo 0
As that line disbales error handling so errors are not
reported.

There is probably a problem with this code as well:
Set objword = Nothing
Word.Application.Quit
objword has been set to an instance of Word earlier, so
the first line releases the reference, but the second is
trying to close Word but there is no reference to it.

I use the following code which works fine for me.

Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim Wasrunning As Boolean
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
Wasrunning = True
If objWord Is Nothing Then
Set objWord = CreateObject("Word.Application")
Wasrunning = False
End If
On Error GoTo Errprint
objWord.Visible = False
Set objDoc = objWord.Documents.Add("C:\windows\application
data\microsoft\templates\u3a.dot")
'Put your code here
objDoc.Close (wdDoNotSaveChanges)'Change this as necessary
Set objDoc = Nothing
If Wasruuning=False Then
objWord.Quit
Set objWord = Nothing
End If
Exit Sub
Errprint:
'Error code goes here
Exit Sub
End Sub

Dave
 
G

Geoff Tegerdine

Dave,
Thank you for your prompt response. Apart from a minor typo (Wasruuning
instead of Wasrunning) everything worked fine. Thank you
Geoff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top