PDA

View Full Version : Run code after item sent



mdmackillop
08-05-2008, 09:52 AM
I've created a userform that enables me to file an item from the Sent Items folder. The calling and initialise code is as follows


Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Shows
End Sub


Sub Shows()
ufSentItems.Show
End Sub

Private Sub UserForm_Initialize()
Dim myOlApp As Outlook.Application
Dim myNamespace As NameSpace
Dim olItem As MailItem
Dim myFolder As Outlook.MAPIFolder

Set myOlApp = CreateObject("Outlook.Application")
Set myNamespace = myOlApp.GetNamespace("MAPI")
'get mail item from Sent Items folder
Set myFolder = _
myNamespace.GetDefaultFolder(5) 'Sent items folder
Set olItem = myFolder.Items(1)
Label1.Caption = olItem

Set myOlApp = Nothing
Set myNamespace = Nothing
Set myFolder = Nothing
Set olItem = Nothing
End Sub



The problem appears to be that the userform is opened before the mail has reached the Sent Items folder, giving me an error on this line.

Set olItem = myFolder.Items(1)
Is there a way to delay things until the item arrives, or a different event I can use?

Mavyak
08-05-2008, 10:45 AM
Can you change this:
Set olItem = myFolder.Items(1)
to this:
Set olItem = Item
Since the item in question is passed to the event procedure? Of course you'll have to forward the "Item" object variable from the event procedure to the Shows() procedure to the form initialization procedure.

Edit: You will likely have to pass the variable ByRef. I'm not sure if you can overload an event in VBA like that, though.

mdmackillop
08-05-2008, 11:20 AM
I've added mySentItem as a Public Variable. That allowed me to open the form and run the code. Unfortunately the ItemSend event is triggered before the item is sent, and the Send does not complete because the item has been moved.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Set mySentItem = Item
Shows
End Sub



I've exported the userform and code to the zip file

mdmackillop
08-05-2008, 11:21 AM
Email setup

Mavyak
08-05-2008, 03:14 PM
Can you add something like:
While Not myItemSent.Sent
DoEvents()
WEnd

I'm writing off the cuff. I'm not usre if the Sent property is boolean or not, but I suspect you get the idea.

mdmackillop
08-06-2008, 12:34 AM
The ItemSend code is probably the wrong approach. It is dealing with the Item before it is sent. I don't see an AfterSending event, so I guess what I need is a workaround. Is there an OnTime type function available in Outlook.
If all else fails, I can click a button, but that seems like admitting defeat!

Mavyak
08-06-2008, 06:43 AM
Dim myOlApp As New Outlook.Application
Public WithEvents myOlItems As Outlook.Items

Public Sub Initialize_handler()
Set myOlItems = myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items
End Sub

Private Sub myOlItems_ItemAdd(ByVal Item As Object)
MsgBox Item.subject
End Sub
Put that code in your ThisOutlookSession class module. Send yourself a test email and the subject should pop up in a message box.

Enjoy!

Mav

Edit: You might want to change the passing style to ByRef. I'm not sure if the default style of ByVal will actually move the item or copy it.

mdmackillop
08-11-2008, 03:36 PM
Hi Mavyak,
I've not forgotten this, just can't get it to work yet. I've looked at the ItemAdd method so see where you are coming from, but the AddItem code isn't getting triggered. Does it work for you?
Regards
MD

Mavyak
08-11-2008, 06:05 PM
Yeah, it sure does. I tested it on me before I posted it to you.

Mavyak
08-11-2008, 06:27 PM
I had to manually run the "Initialize_handler()" sub but it was automatic there after.

mdmackillop
08-12-2008, 05:58 AM
Success at last!
I don't see what iI was doing wrong before, but my final code is as follows


Option Explicit

Dim myOlApp As New Outlook.Application
Public WithEvents myOlItems As Outlook.Items

Private Sub Application_Startup()

'Sent emails handler
Set myOlItems = myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items

'Autoclose routine
If Time < TimeValue(AutoCloseTime) Then StartAutoCloseTimer
End Sub

Private Sub Application_Quit()
StopAutoCloseTimer
End Sub

Private Sub myOlItems_ItemAdd(ByVal Item As Object)
'open the mail handler form
Call Shows
End Sub