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