PDA

View Full Version : Apply macro to new item in Sent Items folder



scoobydoo74
06-12-2015, 04:52 AM
Hi,

I am using the code below to open a userform when an email is added to the sent items folder in Outlook 2013

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
Dim Ns As Outlook.NameSpace
Set Ns = Application.GetNamespace("MAPI")
'use the default folder
Set Items = Ns.GetDefaultFolder(olFolderSentMail).Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
UserForm3.Show
End Sub

This code works fine and opens the userform. On the userform there are several buttons that I want be apply to apply to the item that landed in the sent item folder causing the userform to be opened e.g. on of the buttons will delete the email, another will save the email in MSG format in a specific folder etc. The problem I am having is how to 'select' the email to apply the other macros to.

Any help would be appreciated as I'm new to coding and struggling with this one.

Thanks

scoobydoo74
06-12-2015, 01:11 PM
I'm fully sorted by using the sort function, sorting by ascending senton date and then selecting the first item

gmayor
06-12-2015, 11:50 PM
Is there a reason why you are not using

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

This code in the ThisOutloookSession folder will intercept the Send command and run your code with 'Item' being the message being sent.

scoobydoo74
06-13-2015, 05:24 AM
Using itemsend wouldn't work for what I am trying to do. Using the itemsend function would result in the code applying to the message before it is sent. As my code saves the message in MSG format I need the code to be applied after the message has been sent hence why I'm using itemadd

skatonni
07-22-2015, 09:24 AM
Try declaring a global variable in a standard module.

Option Explicit

Public userformItem As Object

Private Sub Items_ItemAdd(ByVal Item As Object)
Set userformItem = Item
UserForm3.Show
Set userformItem = Nothing
End Sub