Cannot Get Word To Quit

abbaddon223

Registered User.
Local time
Today, 09:08
Joined
Mar 13, 2010
Messages
162
Hi experts,

I have the below code which is opening a word doc and merging it.

The last two lines of the proceedure are really killing me now

oApp.ActiveDocument.Close
oApp.ActiveDocument.Quit

The first will close the document, but the second errors and says there is no document to quit ?! (Arrrrggghhhh!! :banghead:)

Can anyone help me please?



Private Sub Command2_Click()
Dim LWordDoc As String
Dim oApp As Object

'Path to the word document
LWordDoc = "D:\Remote Applications\AIA Invoice Management\Merges\8 - 14 Days Chase Letter.docx"

If Dir(LWordDoc) = "" Then
MsgBox "Document not found."

Else
'Create an instance of MS Word
Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = True

'Open the Document
oApp.Documents.Open FileName:=LWordDoc

Set myMerge = ActiveDocument.MailMerge
If myMerge.State = wdMainAndSourceAndHeader Or _
myMerge.State = wdMainAndDataSource Then
With myMerge.DataSource
.FirstRecord = 1
.LastRecord = 100000
End With
End If

With myMerge
.Destination = wdSendToPrinter
.Execute
End With

oApp.ActiveDocument.Close
oApp.ActiveDocument.Quit

End If


End Sub
 
What about oApp.Quit or oApp.Close instead of oApp.ActiveDocument.Quit?
 
Try this:
oApp.ActiveDocument.Close
Set oApp = Nothing
Kill oApp

David
 
Try this:
oApp.ActiveDocument.Close
Set oApp = Nothing
Kill oApp

David
 
JHB is right. Use oApp.Quit

Once you close the active document there is not an active document anymore (unless you have multiple files open). I would suggest just doing the oApp.Quit method and commenting out your close command. The document will close if the application quits.
 
.oApp.Quit nailed it - thanks for all the help that's really great!!

I finally a fully working mail merge - only taken me 3 weeks to figure out!! :p
 

Users who are viewing this thread

Back
Top Bottom