PDA

View Full Version : Can't remove prompt asking to save changes



sharky12345
01-22-2015, 11:09 AM
I'm using this to generate an email from Word;


Sub EmailReturn()
Dim OutApp As Object
Dim OutMail As Object
Set Doc = ActiveDocument
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
.Subject = "Advice Return"
.To = "Reviews Mailbox"
.CC = ""
.Importance = 2 'Importance Level 0=Low,1=Normal,2=High
.Body = "Please find attached a Return for your information." & vbCrLf _
& vbCrLf & "Please save this file to a suitable folder before opening to ensure full functionality."
.Attachments.Add Doc.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Once the email is generated then the following is run;


Application.Quit SaveChanges:=wdSaveChanges

What should happen is that the email is generated and the document itself closed in the background, leaving the user to check the email and send if they wish.

The issue I'm having is that once the email is generated I get a prompt asking if I want to save changes with 3 options, YES, NO or CANCEL.

If I click CANCEL then the prompt disappears leaving both the email and the document open. If I click NO then it closes the email but not the document, if I click YES then it gives me a message saying there have been changes made to the global template, (or something like that at least!).

I should point out the following which may assist in finding a solution;

I have created the document using Word 2007 on a Windows 7 PC - it works perfectly as expected in that scenario, however, when I deploy it to a work PC which is Office 2003 on Windows XP, that's when I get these issues.

Could it be something like Outlook using Word as it's editor? If so I'd like to know if there is a workaround because I won't be able to account if the end user has that enabled or not.

arangogs
01-22-2015, 01:10 PM
Hi Sharky,

you could try the following:-

ActiveDocument.SaveAs (ActiveDocument.Path)
Application.Quit

this will work, unless you are opening the word document from the code, as it will have no location. otherwise, job done.

gmayor
01-23-2015, 06:09 AM
The code should work in 2003, regardless of whether Outlook 2003 is configured to use its own editor or Word. You can test for that, but it is not necessary unless you want to use the Word Inspector to edit the message body. It is the other code related to the process that is at issue and you have not listed that. In this instance I would simply add the document handling code into the macro, e.g.



Sub EmailReturn()
Dim wrdApp As Word.Application
Dim OutApp As Object
Dim OutMail As Object
Dim Doc As Document
Dim wState As Long
On Error Resume Next
Set wrdApp = Word.Application
If wrdApp.Documents.Count = 0 Then
MsgBox "No document open?"
GoTo lbl_exit
End If
Set Doc = wrdApp.ActiveDocument
wState = wrdApp.WindowState
wrdApp.WindowState = wdWindowStateMinimize
Doc.Save
If Len(Doc.Path) = 0 Then
MsgBox "The document must be saved first"
GoTo lbl_exit
End If
Set OutApp = GetObject(, "Outlook.Application")
If OutApp Is Nothing Then Set OutApp = CreateObject("Outlook.Application")
If OutApp Is Nothing Then
MsgBox "Outlook not available"
GoTo lbl_exit
End If
On Error GoTo 0
Set OutMail = OutApp.CreateItem(0)
With OutMail
.Subject = "Advice Return"
.To = "someone@somewhere.com"
.CC = ""
.Importance = 2 'Importance Level 0=Low,1=Normal,2=High
.Body = "Please find attached a Return for your information." & vbCrLf _
& vbCrLf & "Please save this file to a suitable folder before " & _
"opening to ensure full functionality."
.Attachments.Add Doc.FullName
.Display
End With
Doc.Close SaveChanges = False
lbl_exit:
Set OutMail = Nothing
Set OutApp = Nothing
Set Doc = Nothing
Set wrdApp = Nothing
Exit Sub
End Sub

sharky12345
01-23-2015, 08:20 AM
Graham - that's really helpful, thank you - just for the record I tried the suggestion posted by arangogs but that didn't work - thanks to both though.