PDA

View Full Version : How do I Close Word ?



vodkasoda
02-05-2013, 11:53 AM
I have a button in a Word Document that executes the following code ... the first three steps work, the Word document is saved, it is E:Mailed and it is closed, but that leaves Word still running ... now do I close Word ?!?

Sub myButtonMacro(control As IRibbonControl)

ActiveDocument.Save
SendToChris
ActiveDocument.Close
' The Word Document has been sent & has been closed, but Word itself is still open ...

End Sub

Sub SendToChris()

Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next

Set oOutlookApp = GetObject(, "Outlook.Application")

If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
.To = "mytest@gmail.com"
.Subject = "NewsLetter " + ActiveDocument.Name

'Add the document as an attachment, you can use the .displayname property
'to set the description that's used in the message
.Attachments.Add Source:=ActiveDocument.FullName, Type:=olByValue, _
DisplayName:="Document as attachment"
.Send
End With

If bStarted Then
oOutlookApp.Quit
End If

Set oItem = Nothing
Set oOutlookApp = Nothing

End Sub

fumei
02-05-2013, 03:11 PM
If the code is in the document, then obviously once you use .Close there is no more code to run - Word is still open.

If the code is NOT in the document - it is in Normal, or in a global add-in - then adding .Quit will shut down Word.Sub myButtonMacro(control As IRibbonControl)

ActiveDocument.Save
SendToChris
ActiveDocument.Close
' The Word Document has been sent & has been closed, but Word itself is still open ...
Application.Quit
End Sub

vodkasoda
02-06-2013, 01:57 AM
Works perfectly fumei, thank you :friends:

fumei
02-06-2013, 12:09 PM
You're welcome.