PDA

View Full Version : Solved: Application_ItemSend problem



OTWarrior
12-06-2010, 06:14 AM
We have to save emails to the folder of each client (identified by a unique 9 digit number), and I have the following code that works when applicable:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim MItem As Object
Dim nID As String
Dim Folderpath As String
Dim subject As String
On Error GoTo err1

Set MItem = ActiveInspector.CurrentItem

If FindnIDNumber(MItem.subject) = 0 Then Exit Sub
nID = FindnIDNumber(MItem.subject)

subject = Replace(MItem.subject, "/", "-", 1)
subject = Replace(subject, "|", "-", 1)
subject = Replace(subject, ":", "-", 1)

ActiveInspector.WindowState = olMinimized

DEPT = "Case" ' this has been changed to for this posting as this is company information.

Folderpath = MakeFolder(nID )
Folderpath = MakeFolder(nID & DEPT)

If nID <> "" Then
Folderpath = Folderpath & subject & ".msg"
MItem.SaveAs Folderpath, olMSG
MsgBox "Email saved: " & Folderpath, vbInformation
End If
Exit Sub

err1:
End Sub

This saves fine, but it does so before the email is sent. If you open up the file it has saved it has "this item has not been sent" on the menu bar. I have tried using:

item.sent = true
but this ends the procedure.

Is there a way of changing this to be the item as it is sent, not before it is sent?

JP2112
12-10-2010, 07:56 AM
In short, no, because otherwise you wouldn't be able to do things like add BCC. That's why there's a Cancel parameter, because it occurs just before the item is sent.

I'm confused about why you are using the ItemSend Event to do something to the currently opened item, not the item that is being sent.

OTWarrior
12-10-2010, 08:03 AM
The code I have found to work uses currentitem, everything else I have tried doesn't seem to work the way this does.

How would you use the item being sent instead of the current item?

JP2112
12-10-2010, 09:31 AM
Simple -- just refer to the Item object in your code. i.e.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim msg As Outlook.MailItem
If TypeName(Item) = "MailItem" Then
Set msg = Item
MsgBox msg.Subject
End If
End Sub

The above code will display the subject of all outgoing emails.

When you say "We have to save emails to the folder of each client " what do you mean by emails --

the one you are reading, or
the one you are sending?

If you need to save the one you are reading, you'll need a manual procedure (not an event) to deliberately save the email.

If you need to save the one you are sending, use the ItemSend Event to save a copy of the outgoing email.

HTH

OTWarrior
12-21-2010, 03:17 AM
That worked perfectly. Thanks JP :)

PS: It was for sending email. If it is just something that is read, The macro can be called from a button, but mostly it is for the emails that are sent to other people.