How do I programmatically tell Outlook to not save a copy of a message in the Sent Items folder and

  • Thread starter Thread starter Gary
  • Start date Start date
G

Gary

Hello,

I have an add-in VB 6 COM program that forwards emails using Outlook
2000. When a message is forwarded, it is sent to another email address
and then deleted. This is fine and works great. However, after
running the VB 6 code (in a VB Add-in project), I end up with a copy
of the message in my Sent Items folder and another copy of the same
message in the Deleted Items folder. Besides turning off the option
{Tools | Options | Preferences Tab | E-mail Options | uncheck Save
copies of messages in Sent Items folder), can this be done in code on
a per email basis and in code? I also need a way for Outlook to not
move a copy of the message to the Deleted Items folder. How can this
be programmatically done using the Outlook Object model?

In other words, when my COM Add-in program is running and if the user
selects to forward the email to another email address, I would like to
forward the email to another email address without putting a copy of
the message in the Sent Items folder or a copy of the message in the
Deleted Items folder. Normally, Outlook does this, but I am looking
for a way so that it doesn't do it (in the case when my add-in is
running).

The ideal solution would be to turn off the "Save copies of message in
Sent items folder" when an email is being forwarded with my add-in
program and then turn it back on when it is done. Likewise, my add-in
program would also need to delete the item in the Deleted items folder
or find a way to tell Outlook not to save a copy of the message in the
Deleted Items folder.


Does anyone know how to do this? I seem to remember reading that the
properties in (Tools | options | E-mail options) are not
programmatically available, but I could be wrong.

Thanks.
 
Take a look at the MailItem.DeleteAfterSubmit property.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
Hello,

Thanks for the help. I appreciate it.

Your suggestion worked except that now when I delete the original
message, it is moved to the deleted items folder, which isn't what I
want. If I don't delete the original message, it remains in my inbox
folder. I would like to delete the original message after it is
forwarded and to not have a copy of the message in my Deleted Items
folder.

Here is the code block I am using. (In my add-in VB project I display
a nonmodal form with a command button that the user clicks to forward
the email message – not a userform in Outlook). And this works great
except the pesky problem. Possibly, I am doing something wrong.

Private Sub cmdPersonal_Click()
Dim myolapp As outlook.Application
Dim myNameSpace As outlook.NameSpace
Dim myFolder As outlook.MAPIFolder
Dim myItem As outlook.MailItem
Dim FWDItem As outlook.MailItem
Dim strInput As String
Dim myRecipient As outlook.Recipient

On Error GoTo cmdPersonal_Click_Err:


StartPrompt:

strInput = InputBox("Enter email address to where you want to
forward this email message?", "Forward", "", 4755, 6000)

If strInput = "" Then


If MsgBox("Please enter an email address to where you would
like to forward this email." & vbCrLf & vbCrLf & "Email address is
blank or Cancel was clicked." & vbCrLf & vbCrLf & "Would you like to
try again?", vbYesNo) = vbYes Then
GoTo StartPrompt
Else
Exit Sub
End If
Else

Set myolapp = CreateObject("Outlook.Application")
Set myNameSpace = myolapp.GetNamespace("MAPI")





Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItem = myolapp.ActiveInspector.CurrentItem ‘an email is
already opened
Set FWDItem = myItem.Forward
Set myRecipient = FWDItem.Recipients.Add(strInput)

FWDItem.DeleteAfterSubmit = True ‘New code as suggested by
Sue. Thanks!
myItem.DeleteAfterSubmit = True ‘extraneous? Trial and error
to see if it works.
FWDItem.Send ‘now send the message

'delete the original message
ClosedByProgram = True ‘set flag
myItem.Delete
‘clear flag
ClosedByProgram = False
Unload Me
End If
cmdPersonnel_Click_Exit:
On Error Resume Next
If Err <> 0 Then Err.Clear
Exit Sub
cmdPersonal_Click_Err:
MsgBox Error$
Resume cmdPersonnel_Click_Exit
End Sub


So, as you can tell, I have 2 object variables: myItem pointing to the
original email message and FWDItem pointing to the forwarded email
message. After adding your suggestion
FWDItem.DeleteAfterSubmit = True that worked great by not saving a
copy in the Sent Items folder. However, I still end up with a copy of
the email in my Deleted Items folder (as a result of the line
myItem.delete).

Next, I commented out the line myItem.delete to see what would happen.
The message gets forwarded but the original message remained in my
inbox, which isn't what I wanted. So, that is the reason for the line
myItem.Delete. As soon as this line executes, a copy of the message is
put into my Deleted Items box (which is normal I guess). I would like
to unconditionally and permanently delete the original message in the
inbox. The manual way is to hold down the Shift key and push Delete,
but I am doing this through code.

Thanks again for all your help. Maybe there is another property that
I am missing. Any help is most appreciated. I also want to thank
everyone who has helped me.
 
I had a similar problem, I needed the email to NOT show up
in the deleted items folder. If I used MyItem.Delete the
message was moved from the current folder into the Deleted
Items Folder. By running the same code (MyItem.Delete) on
items in the Deleted Items folder the email was totally
deleted. Here's the code I used, hope it helps:

Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Dim x As Long

'delete the email from the deleted items folder
'set a reference to the deleted items folder
Set mySent = myNameSpace.GetDefaultFolder
(olFolderDeletedItems)

'create a delay after the email is moved, allowing time
for it to be created in the deleted items folder
(otherwise the code runs before the email appears in the
delete folder)
For x = 1 To 10000000
Next x

'set a reference to the items in the deleted items folder
Set myItems = mySent.Items

'find all items in the deleted item folder with the
specified subject
Set myItem = myItems.Find("[Subject]='Reassignment
Notification'")

'loop through the found items and delete them.
While TypeName(myItem) <> "Nothing"
myItem.Delete
Set myItem = myItems.FindNext

Wend

'clean up
Set myItem = Nothing
Set myItems = Nothing
Set mySent = Nothing
Set myDestFolder = Nothing
Set m = Nothing
Set myNameSpace = Nothing
Set myOlApp = Nothing
 
Back
Top