Trying to have code close an open email after it saves.

D

DStrong

Using outlook 2003 I am trying to get outlook to close the currently opened
email after it saves it. I have got everything working but the closeing part.
Can someone give me some ideas, suggestions or the write code to get this to
work. Here is what I have at this point:

Sub SaveAsOF()
'## Saves current email to users My Documents and Emails folder
Dim myItem As Outlook.Inspector
Dim objItem As Object

Set myItem = Application.ActiveInspector
If Not TypeName(myItem) = "Nothing" Then
Set objItem = myItem.CurrentItem
strname = objItem.Subject
strInvalidSequences = "`+
+~+!+@+$+%+^+&+*+=+{+}+[+]+|+\+""+:+;+<+>+/"
strArrInvalidSequence = Split(strInvalidSequences, "+")

For x = 0 To UBound(strArrInvalidSequence)
Text = strArrInvalidSequence(x)
strname = Replace(strname, Text, "_")
Next x
strpath = Environ("HOMEdrive") & "My Documents\Emails\" & strname &
".msg"
'Prompt the user for confirmation
Dim strPrompt As String
strPrompt = "The email has been saved as " & strpath
CheckFolder
objItem.SaveAs Environ("HOMEdrive") & "\My Documents\Emails\" &
strname & ".msg", olMSG
MsgBox (strPrompt)
Else
MsgBox "You must open the email to save it, please double click the
email and try again."
End If
End Sub

Sub CheckFolder()
Dim fso
Dim fol As String
fol = Environ("HOMEdrive") & "My Documents\Emails"
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(fol) Then
fso.CreateFolder (fol)
End If
End Sub

Since some people do not have the "Emails" folder the second macro included
here adds it. Thanks in advance for any help on this.
 
K

Ken Slovak - [MVP - Outlook]

You can use the Save event of the item once you have a reference to the item
to do that. Just call the Close method on the item during the Save event.
 
D

DStrong

Thanks for the reply. I have tried adding "Close" at the end of my saveas
statement and it askes for an expression. I have tried "objItem Close",
"Close.objItem", "Close" all after my msgbox and still no luck. I have tried
to read up on the Close method and can't see to figure out how to format it
as well as what you just suggested. I guess that I am putting something in
the wrong place or something.

David

Ken Slovak - said:
You can use the Save event of the item once you have a reference to the item
to do that. Just call the Close method on the item during the Save event.




DStrong said:
Using outlook 2003 I am trying to get outlook to close the currently
opened
email after it saves it. I have got everything working but the closeing
part.
Can someone give me some ideas, suggestions or the write code to get this
to
work. Here is what I have at this point:

Sub SaveAsOF()
'## Saves current email to users My Documents and Emails folder
Dim myItem As Outlook.Inspector
Dim objItem As Object

Set myItem = Application.ActiveInspector
If Not TypeName(myItem) = "Nothing" Then
Set objItem = myItem.CurrentItem
strname = objItem.Subject
strInvalidSequences = "`+
+~+!+@+$+%+^+&+*+=+{+}+[+]+|+\+""+:+;+<+>+/"
strArrInvalidSequence = Split(strInvalidSequences, "+")

For x = 0 To UBound(strArrInvalidSequence)
Text = strArrInvalidSequence(x)
strname = Replace(strname, Text, "_")
Next x
strpath = Environ("HOMEdrive") & "My Documents\Emails\" & strname &
".msg"
'Prompt the user for confirmation
Dim strPrompt As String
strPrompt = "The email has been saved as " & strpath
CheckFolder
objItem.SaveAs Environ("HOMEdrive") & "\My Documents\Emails\" &
strname & ".msg", olMSG
MsgBox (strPrompt)
Else
MsgBox "You must open the email to save it, please double click the
email and try again."
End If
End Sub

Sub CheckFolder()
Dim fso
Dim fol As String
fol = Environ("HOMEdrive") & "My Documents\Emails"
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(fol) Then
fso.CreateFolder (fol)
End If
End Sub

Since some people do not have the "Emails" folder the second macro
included
here adds it. Thanks in advance for any help on this.
 
K

Ken Slovak - [MVP - Outlook]

Just use myItem.Close(olDiscard) since you have that as an Inspector
reference.
 
D

DStrong

Thanks so much Ken, BTW do you have a book for VBA or Outlook? I have Sue's
programming one, but the more I can learn then better. :)

David
 
K

Ken Slovak - [MVP - Outlook]

See my signature :)

My book does have some on VBA and forms but is geared to more advanced
topics such as COM addins and concentrates on Outlook 2007.
 

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