Outlook Banter

Outlook Banter (http://www.outlookbanter.com/)
-   Outlook and VBA (http://www.outlookbanter.com/outlook-vba/)
-   -   Move item between folders (http://www.outlookbanter.com/outlook-vba/81976-move-item-between-folders.html)

Nigel RS November 19th 08 12:19 PM

Move item between folders
 
I currently have some VBA code that runs when an mail item arrives in my
inBox, this is triggered by Inbox Rule. That works fine.

I now need to be able to run some code when a user manually moves a mail
item to a different folder within the MailBox. How can I detect this event?

For example
MailBox
/InBox
... mail item 1
/Test

When mail item 1 arrived a rule triggered some VBA code. If the user now
drags mail item 1 from /InBox to folder /Test, I wish to run some code. This
needs to differentiate 'from where' - 'to where' the item is moved.

Please help!




Ken Slovak - [MVP - Outlook] November 19th 08 02:34 PM

Move item between folders
 
You would need to set up event handling for every folder you are interested
in. The event handling would be for the folder's Items collection and would
handle the ItemAdd() event. You would declare each folder as an
Outlook.MAPIFolder object and then declare the folder's Items collection
WithEvents so you can handle events for that Items collection.

The best way would be to create a class module and have this code in it:

Private WithEvents colItems as Outlook.Items
Private oFolder As Outlook.MAPIFolder

Private Sub colItems_ItemAdd(Item As Object)
' do whatever you want
End Sub

When you initialize an instance of this class module you would set the
oFolder and colItems objects, the event will then fire when items are added
to the folder. To keep the class and its objects alive you just add them to
a Collection object.

In the Application_Startup() event handler in ThisOutlookSession you would
get each folder you want to handle and create an instance of your class, set
the folder and items objects, then add the class to the Collection which
would be declared at class level in ThisOutlookSession:

Private colFolderItems As New Collection


--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Nigel RS" wrote in message
...
I currently have some VBA code that runs when an mail item arrives in my
inBox, this is triggered by Inbox Rule. That works fine.

I now need to be able to run some code when a user manually moves a mail
item to a different folder within the MailBox. How can I detect this
event?

For example
MailBox
/InBox
... mail item 1
/Test

When mail item 1 arrived a rule triggered some VBA code. If the user now
drags mail item 1 from /InBox to folder /Test, I wish to run some code.
This
needs to differentiate 'from where' - 'to where' the item is moved.

Please help!





Nigel RS November 19th 08 03:17 PM

Move item between folders
 
Hi Ken
Thank you very much, but I am not sure I follow everything you said....

In Class1 module I have added the following.....

Private WithEvents colItems As Outlook.Items
Private oFolder As Outlook.MAPIFolder

Private Sub colItems_ItemAdd(Item As Object)
' test to show an action occurs
MsgBox "Mail: " & Item
End Sub

In ThisOutlookSession I have added......

Private Sub Application_Startup()
Private colFolderItems As New Collection
colFolderItems.Add Item:="RSM Data", Key:=CStr(1)
colFolderItems.Add Item:="RSS Data", Key:=CStr(2)
End Sub

What I am not sure about is how to intialize the class module??

I am sure it must be simple!

Cheers




"Ken Slovak - [MVP - Outlook]" wrote:

You would need to set up event handling for every folder you are interested
in. The event handling would be for the folder's Items collection and would
handle the ItemAdd() event. You would declare each folder as an
Outlook.MAPIFolder object and then declare the folder's Items collection
WithEvents so you can handle events for that Items collection.

The best way would be to create a class module and have this code in it:

Private WithEvents colItems as Outlook.Items
Private oFolder As Outlook.MAPIFolder

Private Sub colItems_ItemAdd(Item As Object)
' do whatever you want
End Sub

When you initialize an instance of this class module you would set the
oFolder and colItems objects, the event will then fire when items are added
to the folder. To keep the class and its objects alive you just add them to
a Collection object.

In the Application_Startup() event handler in ThisOutlookSession you would
get each folder you want to handle and create an instance of your class, set
the folder and items objects, then add the class to the Collection which
would be declared at class level in ThisOutlookSession:

Private colFolderItems As New Collection


--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Nigel RS" wrote in message
...
I currently have some VBA code that runs when an mail item arrives in my
inBox, this is triggered by Inbox Rule. That works fine.

I now need to be able to run some code when a user manually moves a mail
item to a different folder within the MailBox. How can I detect this
event?

For example
MailBox
/InBox
... mail item 1
/Test

When mail item 1 arrived a rule triggered some VBA code. If the user now
drags mail item 1 from /InBox to folder /Test, I wish to run some code.
This
needs to differentiate 'from where' - 'to where' the item is moved.

Please help!






Ken Slovak - [MVP - Outlook] November 19th 08 04:32 PM

Move item between folders
 
The declaration for colFolderItems needs to be at class level outside of the
startup procedure so it stays alive after that procedure ends. You'd also
need to have the items you're adding to the collection already instantiated
of course.

To instantiate a Class1 object do this:

Dim oClass As New Class1

You then need to set oClass (Class1) Folder and Items properties so those
should be Public:

' in Class1
Public WithEvents colItems As Outlook.Items
Public oFolder As Outlook.MAPIFolder

Now assuming you have the folders you want as oFolder1 and oFolder2 you'd do
this:

Set oClass.oFolder = oFolder1
Set oClass.colItems = oFolder1.Items

and repeat with a new instance of Class1 for each folder such as oFolder2.

Of course you'd need code to declare and instantiate all the folders you
want to monitor.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Nigel RS" wrote in message
...
Hi Ken
Thank you very much, but I am not sure I follow everything you said....

In Class1 module I have added the following.....

Private WithEvents colItems As Outlook.Items
Private oFolder As Outlook.MAPIFolder

Private Sub colItems_ItemAdd(Item As Object)
' test to show an action occurs
MsgBox "Mail: " & Item
End Sub

In ThisOutlookSession I have added......

Private Sub Application_Startup()
Private colFolderItems As New Collection
colFolderItems.Add Item:="RSM Data", Key:=CStr(1)
colFolderItems.Add Item:="RSS Data", Key:=CStr(2)
End Sub

What I am not sure about is how to intialize the class module??

I am sure it must be simple!

Cheers




All times are GMT +1. The time now is 11:30 AM.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2006 OutlookBanter.com