re-launching Word -> fails to Save document

P

P.Schuman

Running Access 2007 with SharePoint MOSS 2007

Here is a message from a biz friend that has created an invoicing system
using Access 2007 on the local PC, and SharePoint 2007 on the remote server
for file storage.

--
Here is a precise description of the problem.... any URL's have been
sanitized for public reading -



The routine which is enclosed is an Access 2007 routine that launches Word
2007 from within Access Visual Basic.



· It creates a late binding instance of MS Word 2007 from MS ACCESS
2007.

· When the program begins it tries to use GetObject(,"word.Application)
to see if there is an existing instance of word to use.

· If an error occurs, it then uses CreateObject ("word.application")
to launch word. Here is the relevant code:

Set wordobj = GetObject(, "word.application")

If Err.number <> 0 Then

'An error is thrown if word is not running

' So use create object to start word

Set wordobj = CreateObject("word.application")

MsgBox "CreateObject was used"

End If

wordobj.Documents.Add _

Template:="https://sharepoint.xyz.com/ClientInvoices/Invoice-Template/Invoice-Template.dotx",
Newtemplate:=False, Visible:=True

wordobj.Visible = True



· A template is populated with a series of bookmarks based on some
fields from an access data base. This is rather simple and works fine.



· Finally the file is saved using the following two commands. The
first line simply creates a file name. The second line does the save.

filename = "https://sharepoint.xyz.com/ClientInvoices/Invoices/" &
Me.txtNewInvoiceNumber & "-" & Me.txtProject & "-" & Format(Now(),
"mm-dd-yyyy-hh-mm") & ".doc"

ActiveDocument.SaveAs filename:=filename, FileFormat:=wdFormatDocument97



· THIS WORKS PERFECTLY SO LONG AS I DON"T CLOSE WORD.

It will save files over and over per the logic it follows.

Let me repeat - I CAN RUN THIS CODE PERFECTLY A MILLION TIMES SO LONG AS I
DON'T CLOSE WORD.



· HOWEVER IF I ADD THE FOLLOWING COMMAND AT THE END OF THE MODULE

THE CODE FAILS to SAVE. AND I DON"T THINK IT IS THE CODE, BUT MS OFFICE.

wordobj.Quit



· By adding a quit command, MS word ends and must be restarted by
the code.

· If word closes - either through the user closing it or through
this command,

the new instance of WORD created the next time the code is run, -> fails to
Save the document.



· The WORD DOCUMENT file is successfully opened,

the DOCUMENT fields filled in and the word document is fine,

BUT IT WILL NOT SAVE. THE SAVE SUDDENLY FAILS. AND FAILS EVERY TIME.



o As I test it, I get the following system error after the save attempt:
"462 - The remote machine does not exist or is unavailable"



· This should not occur. Yet it does. It appears that when the
access code tries to reestablish the connection to the Word instance, but
it has issues that are not user issues. It executes the createobject and
launches word fine, fills in the document but FAILS the SAVE.



My question is simple - WHY??????? AND HOW DO I FIX THIS.
 
B

boblarson

Have you tried using

wordObj.SaveAs

instead of ActiveDocument?
--
Bob Larson
Access World Forums Super Moderator
Utter Access VIP
Tutorials at http://www.btabdevelopment.com
__________________________________
If my post was helpful to you, please rate the post.
 
R

RoyVidar

P.Schuman said:
Running Access 2007 with SharePoint MOSS 2007

Here is a message from a biz friend that has created an invoicing system
using Access 2007 on the local PC, and SharePoint 2007 on the remote server
for file storage.

--
Here is a precise description of the problem.... any URL's have been
sanitized for public reading -



The routine which is enclosed is an Access 2007 routine that launches Word
2007 from within Access Visual Basic.



· It creates a late binding instance of MS Word 2007 from MS ACCESS
2007.

· When the program begins it tries to use GetObject(,"word.Application)
to see if there is an existing instance of word to use.

· If an error occurs, it then uses CreateObject ("word.application")
to launch word. Here is the relevant code:

Set wordobj = GetObject(, "word.application")

If Err.number <> 0 Then

'An error is thrown if word is not running

' So use create object to start word

Set wordobj = CreateObject("word.application")

MsgBox "CreateObject was used"

End If

wordobj.Documents.Add _

Template:="https://sharepoint.xyz.com/ClientInvoices/Invoice-Template/Invoice-Template.dotx",
Newtemplate:=False, Visible:=True

wordobj.Visible = True



· A template is populated with a series of bookmarks based on some
fields from an access data base. This is rather simple and works fine.



· Finally the file is saved using the following two commands. The
first line simply creates a file name. The second line does the save.

filename = "https://sharepoint.xyz.com/ClientInvoices/Invoices/" &
Me.txtNewInvoiceNumber & "-" & Me.txtProject & "-" & Format(Now(),
"mm-dd-yyyy-hh-mm") & ".doc"

ActiveDocument.SaveAs filename:=filename, FileFormat:=wdFormatDocument97



· THIS WORKS PERFECTLY SO LONG AS I DON"T CLOSE WORD.

It will save files over and over per the logic it follows.

Let me repeat - I CAN RUN THIS CODE PERFECTLY A MILLION TIMES SO LONG AS I
DON'T CLOSE WORD.



· HOWEVER IF I ADD THE FOLLOWING COMMAND AT THE END OF THE MODULE

THE CODE FAILS to SAVE. AND I DON"T THINK IT IS THE CODE, BUT MS OFFICE.

wordobj.Quit



· By adding a quit command, MS word ends and must be restarted by
the code.

· If word closes - either through the user closing it or through
this command,

the new instance of WORD created the next time the code is run, -> fails to
Save the document.



· The WORD DOCUMENT file is successfully opened,

the DOCUMENT fields filled in and the word document is fine,

BUT IT WILL NOT SAVE. THE SAVE SUDDENLY FAILS. AND FAILS EVERY TIME.



o As I test it, I get the following system error after the save attempt:
"462 - The remote machine does not exist or is unavailable"



· This should not occur. Yet it does. It appears that when the
access code tries to reestablish the connection to the Word instance, but
it has issues that are not user issues. It executes the createobject and
launches word fine, fills in the document but FAILS the SAVE.



My question is simple - WHY??????? AND HOW DO I FIX THIS.

When you're automating another application, you must "anchor" any
objects, properties or methods belonging to that application to
some parent object of that automated application.

Using

ActiveDocument.SaveAs ...

will causes such error as you experience (or other kind of automation
errors), and should have been

wordobj.ActiveDocument.SaveAs ...

Personally, I would declare and instantiate an object variable for
the document, i e

set oDoc = wordobj.Documents.Add ...

oDoc.SaveAs ...

If you want to close word, I think you should probably also close the
document, too, before quitting.

Another thing, you are sure you're using late binding? Using one of
the Word constants (wdFormatDocument97), suggests you're usin early
binding, or keep a reference to Word, unless you've declared it as a
private or public constant.

If these suggestions doesn't fix the issue, you will need to look
through the code working with Word objects, properties and methods
to remove any other unqualified referencing. There's some more info
here http://support.microsoft.com/default.aspx?kbid=178510
 
P

P.Schuman

here's another simple problem report from 2003 & suggested solution -
I passed it back t my biz friend,
and he tried a few changes, and his app seems to work ok now
for the situation when Word is closed & re-opened.

------------------->>>


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
----------------------


Newsgroups: microsoft.public.access.gettingstarted
From: "Dave Jones" <[email protected]>
Date: Sat, 18 Oct 2003 08:36:38 -0700
Local: Sat, Oct 18 2003 9:36 am

Subject: Re: Opening Word files from Access

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 Wasrunning=False Then
objWord.Quit
Set objWord = Nothing
End If
Exit Sub
Errprint:
'Error code goes here
Exit Sub
End Sub
 

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