Closing a word document from Internet explorer

M

Madalena Urena

How can I close a word document from a macro after the
macro has finish executing. I try the following code but
it does not work. The word program seems to lose the focus
and I get an error message -
Run-time error '4605':
This method or property is not available because the
document is in another application.

Selection.Paste

ActiveDocument.SaveAs
FileName:="c:\TXT\Test2_0001.doc", FileFormat:= _
wdFormatDocument, LockComments:=False,
Password:="", AddToRecentFiles:= _
True, WritePassword:="",
ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False,
SaveFormsData:=False, _
SaveAsAOCELetter:=False
Application.Quit


***********************************************************
I also Try the following code.

Dim oMSWord As Object


On Error GoTo ErrHandler

Selection.Paste

ActiveDocument.SaveAs
FileName:="c:\TXT\Test2_0001.doc", FileFormat:= _
wdFormatDocument, LockComments:=False,
Password:="", AddToRecentFiles:= _
True, WritePassword:="",
ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False,
SaveFormsData:=False, _
SaveAsAOCELetter:=False
Application.Quit


ErrHandler:
If Err <> 0 Then
' If Word is already running, use the existing
instance.
Set oMSWord = GetObject(Class:="Word.Application")
If Err <> 0 Then
' If Word is NOT already running, create a new
instance.
Set oMSWord = CreateObject
(Class:="Word.Application")
End If
If oMSWord.Visible = False Then ' Make Word
visible.
oMSWord.Visible = True
End If
oMSWord.Activate ' <--- Activate Word
application.
oMSWord.Quit

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