PDA

Click to See Complete Forum and Search --> : Outlook Rules Wizard / Custom Action


CVMichael
Jun 27th, 2005, 04:28 PM
I made a folder in Outlook "Sent Items" folder, say folder name "ABC".
I want to MOVE any item that is in Sent Items (that is sent to certain people) to that ABC folder.
The problem is that in the rules, I have only "move a copy"... I don't want a copy of it, I want it MOVED !
It's anoying because I have the option to move an email when I receive an item, but not when I send :mad:

So, anyone knows how to do it ?

Also, if it cannot be done, I saw something like "Custom Action" in the wizard, and I found this on the net:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/exchserv/html/compnent_9wxf.asp
But that does not really help me, not enough info, and where IS the sample application ? (CRARUN ?)

RobDog888
Jun 27th, 2005, 04:35 PM
What version of Outlook are you running? You could just write some VBA code to move sent items to your subfolder.

RobDog888
Jun 27th, 2005, 04:47 PM
This should do what you need. Allot easier then fighting with the rules wiz.
Place this in Outlooks VBA editor (Alt+F11).
You will need to exit and logoff of Outlook and restart it up again. Make sure macros are enabled.
'ThisOutlookSession
Option Explicit

Public WithEvents SentItemsAdd As Items

Private Sub Application_MAPILogonComplete()
Set SentItemsAdd = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items
End Sub

Private Sub SentItemsAdd_ItemAdd(ByVal Item As Object)
If Item.Subject = "ABC" Then
Dim oSubFolder As Outlook.MAPIFolder
Set oSubFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Folders("ABC")
Item.Move oSubFolder
Set oSubFolder = Nothing
End If
End SubVB/Outlook Guru™ :D

CVMichael
Jun 27th, 2005, 04:47 PM
Outlook 2002 (10.2627.3311)

How do you write VBA for Outlook ?

CVMichael
Jun 27th, 2005, 04:53 PM
Thanks for the code, that works for the Subject, I assume that if I want to move only if I sent to "Smith" then I would do something like:

If Item.To Like "*Smith*" Then

I will send more e-mails tomorow, so I can't test right now... but it looks simple enough, so it should work...

RobDog888
Jun 27th, 2005, 04:57 PM
Yes, that will work and no Outlook Security prompt since its using the Trusted Application object in VBA.

CVMichael
Jun 27th, 2005, 05:03 PM
By the way, by this:
Make sure macros are enabled.
You mean the security setting ?

[edit], nevermind, you already ansered my question

RobDog888
Jun 27th, 2005, 05:07 PM
:D Just make sure its on medium since its an unsigned vba project.

RobDog888
Jun 27th, 2005, 05:14 PM
Check out my Tutorial on creating a Digital Signature (http://www.vbforums.com/showthread.php?t=285212&highlight=digital+signature) for a VBA project. This will enable you to sign your
Outlook project and set the security level to High and get rid of the irritating "Enable Macros" message when you
startup Outlook each time. ;)

CVMichael
Jun 27th, 2005, 05:15 PM
By the way, is there a way to Always "Enable Macros" ?

CVMichael
Jun 27th, 2005, 05:16 PM
Do you ALWAYS read my mind ??? :D :D

RobDog888
Jun 27th, 2005, 05:16 PM
I knew that was coming. See post #9 :D

RobDog888
Jun 27th, 2005, 05:17 PM
:lol: I guess I'm just having a good day today. :thumb:'s for the Scooby Snack. ;)

CVMichael
Jun 27th, 2005, 05:34 PM
Thanks, the Digital Signature worked ! :)

Now I have another question (since we are at this :)):
I made a macro in an Excel file wich I sent to someone, and that person has to click on "Enable Marcos" every time.
Now if I add the Digital Signature I just made to that Excel file, when I give the Excel file to that person, does the Digital Signature stay with the file ? I mean, will it apply on that computer also ?

RobDog888
Jun 27th, 2005, 05:40 PM
No, it will not travel with the Excel file. It is VERY bad to distribute a private digital signature since anyone can use it to write
a virus (signed with your signature) and have you get in trouble. You can get a Public digital signature from a few companies,
like Verisign for ex., but its about $400 a year. Too much for a non-comercial macro. If the other user is going to be able to
view your code then you can send them the link to my Tutorial and they can create their own DS to sign your project with.
This way if they modify it or it ends up doing harm, your off the hook since the DS is generated off of a hardware hash of
their system. ;)

CVMichael
Jun 27th, 2005, 05:46 PM
I see...

Thanks again for everything... (no more questions :)).... yet.... :)

RobDog888
Jun 27th, 2005, 05:49 PM
Your Welcome. :)

Its been a while since I have done any Outlook programming (burned out). Its about time I got back into it since MS is going to release
Visual Studio Tools for Office 2005 - Outlook Add-In soon (can't wait. He he :D)

Oh, and late next year we may see Office 12! :D

CVMichael
Jun 28th, 2005, 11:46 AM
Hi Rob, now i'm getting this whenever I send something (or every 10 minutes, if I choose "Allow access for 10 minues")

Do you know how to get rid of that window ?

By the way, I'm also making a program that reads e-mails from Outlook, and in that program I'm also getting this window to allow access.

Is there a way to Always allow access for the application I choose (kinda like a firewall) ?

RobDog888
Jun 28th, 2005, 01:19 PM
Hmm, I thought that that wouldnt happen since the code is in the ThisOutlook session. Did you sign the VBA Project and
is your DS showing as a Trused Cert? It should not have the red circle with an 'x' in it.

Also, in Outlook you need to check always trust all addins and templates.

This is happening because of the .To property being accessed.

CVMichael
Jun 28th, 2005, 08:35 PM
Hmm, I thought that that wouldnt happen since the code is in the ThisOutlook session. Did you sign the VBA Project and is your DS showing as a Trused Cert? It should not have the red circle with an 'x' in it.

Yea I signed it, no red circle with x.

Also, in Outlook you need to check always trust all addins and templates.

How do I do that ?

RobDog888
Jun 28th, 2005, 09:09 PM
Tools > Macro > Security...

ewall
Jul 15th, 2005, 04:12 PM
Thanks for the code snippet, RobDog888...

One question on extending it, however (since I've coded plenty of VBA & VBS, but never for Outlook): Is is easy enough to have it present a "Browse..." box of the possible folders to drop it in?

What I'm looking for is a way to set up my Outlook such that every time I send a message, it immediately prompts me to either (a) delete the "Sent Item" message or (b) move the "Sent Item" to a folder of my choice.

Need to get some Outlook programming references now--the possibilities are endless!

RobDog888
Jul 15th, 2005, 04:15 PM
Welcome to the Forums.

I'm glad you like my code. :)

Yes, you can get Outlook to popup its MAPIFolder browse dialog box. Do you need it in Outlooks VBA of from VB6/.NET?

RobDog888
Jul 15th, 2005, 05:28 PM
Here is some VB6 code wich is easily modifiable to VBA for what you asked. ;)
Option Explicit
'Add a reference to MS Outlook xx.0 Object Library
Private Sub Command1_Click()

On Error GoTo MyError

Dim oApp As Outlook.Application
Dim oNS As Outlook.NameSpace
Dim oSource As Outlook.MAPIFolder
Dim oDestination As Outlook.MAPIFolder
Dim oEmail As Object
Dim lRetVal As VbMsgBoxResult

Set oApp = New Outlook.Application
Set oNS = oApp.GetNamespace("MAPI")
Set oSource = oNS.GetDefaultFolder(olFolderSentMail)

Set oDestination = oNS.PickFolder
If Not oDestination Is Nothing Then
If oDestination.DefaultMessageClass <> "IPM.NOTE" And oDestination.DefaultItemType = olMailItem Then
'Do your move stuff here
'Get the latest Sent Item for moving/Deleting
oSource.Items.Sort "[Created]", True
Set oEmail = oSource.Items(1)
lRetVal = MsgBox("Do you want to Move or Delete '" & oEmail.Subject & "'?" & vbNewLine & "Click 'Yes' to Move and 'No' to delete!", vbYesNoCancel, App.ProductName)
If lRetVal = vbYes Then
'Move it
oEmail.Move oDestination
ElseIf lRetVal = vbNo Then
'Find the item in the colection (latest sent item) and delete it
oSource.Items(1).Delete
Else
'Cancel
End If
Else
MsgBox "Invaild 'Destination' folder type!" & vbNewLine & "Folder must be a 'MailItem' type.", vbExclamation + vbOKOnly, App.ProductName
End If
End If
Set oEmail = Nothing
Set oSource = Nothing
Set oDestination = Nothing
Set oNS = Nothing
Set oApp = Nothing
Exit Sub

MyError:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbInformation, App.ProductName
End Sub

ewall
Jul 17th, 2005, 07:43 PM
Yes, inside Outlook is what I was looking for...

The example code looks great--I'll try it tomorrow when I get back to work!

ewall
Jul 20th, 2005, 09:30 AM
RobDog888, you rock--this is great!

So I tried hooking up the above inside the ThisOutlookSession, with the events used in the other code example above, with a few minor changes:

Had to replace the App.ProductName references with oApp.Name.
Re-arranged so that the prompt was first, then the move or delete bits.


Here's my almost-working result:
Option Explicit

Public WithEvents SentItemsAdd As Items

Private Sub Application_MAPILogonComplete()
Set SentItemsAdd = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items
End Sub

Private Sub SentItemsAdd_ItemAdd(ByVal Item As Object)
' On Error GoTo MyError

Dim oApp As Outlook.Application
Dim oNS As Outlook.NameSpace
Dim oSource As Outlook.MAPIFolder
Dim oDestination As Outlook.MAPIFolder
Dim oEmail As Object
Dim lRetVal As VbMsgBoxResult

Set oApp = New Outlook.Application
Set oNS = oApp.GetNamespace("MAPI")
Set oSource = oNS.GetDefaultFolder(olFolderSentMail)

'Get the latest Sent Item for moving/deleting
oSource.Items.Sort "[Created]", True
Set oEmail = oSource.Items(1)

lRetVal = MsgBox("Do you want to Move or Delete '" & oEmail.Subject & "'?" & vbNewLine & "Click 'Yes' to Move and 'No' to delete!", vbYesNoCancel, oApp.Name)
If lRetVal = vbYes Then
'Move it
Set oDestination = oNS.PickFolder
If Not oDestination Is Nothing Then
If oDestination.DefaultMessageClass <> "IPM.NOTE" And oDestination.DefaultItemType = olMailItem Then
oEmail.Move oDestination
End If
Else
MsgBox "Invaild 'Destination' folder type!" & vbNewLine & "Folder must be a 'MailItem' type.", vbExclamation + vbOKOnly, oApp.ProductName
End If
ElseIf lRetVal = vbNo Then
'Delete it
oSource.Items(1).Delete
Else
'Cancel
End If

Set oEmail = Nothing
Set oSource = Nothing
Set oDestination = Nothing
Set oNS = Nothing
Set oApp = Nothing
Exit Sub
MyError:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbInformation, oApp.Name
End Sub

One big bug remains: It picks the wrong email from the Sent Items! Perhaps it's something to do with this line?:
oSource.Items.Sort "[Created]", True
Any ideas?

RobDog888
Jul 20th, 2005, 10:43 AM
Try changing it to False so it will reverse the sorting direction. Not sure if my dyslexia is an issue here. :)

oSource.Items.Sort "[Created]", False

ewall
Jul 20th, 2005, 01:01 PM
I tried "False" to reverse the sort order, and it came up with the same seemingly random email from the middle of my sent items. I also tried commenting out the Sort method line, and it selects the same random email... methinks the Sort method isn't doing anything.

Instead, I had it go to the last one in the Items collection, and it worked:
Set oEmail = oSource.Items(oSource.Items.Count)
...then I had to fix the line which deletes the email, which also used the Items collection.

The final, working code is this:
Option Explicit

Public WithEvents SentItemsAdd As Items

Private Sub Application_MAPILogonComplete()
Set SentItemsAdd = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items
End Sub

Private Sub SentItemsAdd_ItemAdd(ByVal Item As Object)
' On Error GoTo MyError

Dim oApp As Outlook.Application
Dim oNS As Outlook.NameSpace
Dim oSource As Outlook.MAPIFolder
Dim oDestination As Outlook.MAPIFolder
Dim oEmail As Object
Dim lRetVal As VbMsgBoxResult
Dim lItemNumber As Long

Set oApp = New Outlook.Application
Set oNS = oApp.GetNamespace("MAPI")
Set oSource = oNS.GetDefaultFolder(olFolderSentMail)

'Get the latest Sent Item for moving/deleting
lItemNumber = oSource.Items.Count
Set oEmail = oSource.Items(lItemNumber)

lRetVal = MsgBox("Do you want to Move or Delete '" & oEmail.Subject & "'?" & vbNewLine & "Click 'Yes' to Move and 'No' to delete!", vbYesNoCancel, oApp.Name)
If lRetVal = vbYes Then
'Move it
Set oDestination = oNS.PickFolder
If Not oDestination Is Nothing Then
If oDestination.DefaultMessageClass <> "IPM.NOTE" And oDestination.DefaultItemType = olMailItem Then
oEmail.Move oDestination
End If
Else
MsgBox "Invaild 'Destination' folder type!" & vbNewLine & "Folder must be a 'MailItem' type.", vbExclamation + vbOKOnly, oApp.ProductName
End If
ElseIf lRetVal = vbNo Then
'Delete it
oSource.Items(lItemNumber).Delete
Else
'Cancel
End If

Set oEmail = Nothing
Set oSource = Nothing
Set oDestination = Nothing
Set oNS = Nothing
Set oApp = Nothing
Exit Sub
MyError:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbInformation, oApp.Name
End Sub

Woo-hoo! Thanks for your help--

RobDog888
Jul 20th, 2005, 01:09 PM
Glad its working the way you need now. :thumb:

I tried the .Count before I posted earlier but depending on the sorting of the view for my Inbox it would return different items.
Becareful that it doesnt change on you. ;) I am running 2003 btw.

Gangsta Yoda http://www.vbforums.com/attachment.php?attachmentid=38679

ewall
Jul 22nd, 2005, 01:52 PM
I was playing with another folder event today and realized that this code could be trimmed significantly... so I had to clean it up a bit.

At first I relized that instead of using the .Count property, there's the .GetLast method. (It's amazing what you might find when you actually use the help reference.)

Then I noticed that, when called inside ThisOutlookSession, the event passes a reference to the email item anyway! (In the definition of Private Sub SentItemsAdd_ItemAdd(ByVal Item As Object) ). So there was much cleaning and no worries about getting the wrong email anymore.

Here's my final, final. Really. I mean it this time. :p

Option Explicit

Public WithEvents SentItemsAdd As Items

Private Sub Application_MAPILogonComplete()
Set SentItemsAdd = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items
End Sub

Private Sub SentItemsAdd_ItemAdd(ByVal Item As Object)
' On Error GoTo MyError

Dim oApp As Outlook.Application
Dim oNS As Outlook.NameSpace
Dim oDestination As Outlook.MAPIFolder
Dim lRetVal As VbMsgBoxResult

Set oApp = New Outlook.Application
Set oNS = oApp.GetNamespace("MAPI")

lRetVal = MsgBox("Do you want to Move or Delete '" & Item.Subject & "'?" & vbNewLine & "Click 'Yes' to Move and 'No' to delete!", vbYesNoCancel, "Sent Items")
If lRetVal = vbYes Then
'Move it
Set oDestination = oNS.PickFolder
If Not oDestination Is Nothing Then
If oDestination.DefaultMessageClass <> "IPM.NOTE" And oDestination.DefaultItemType = olMailItem Then
Item.Move oDestination
End If
Else
MsgBox "Invaild 'Destination' folder type!" & vbNewLine & "Folder must be a 'MailItem' type.", vbExclamation + vbOKOnly, "Sent Items"
End If
ElseIf lRetVal = vbNo Then
'Delete it
Item.Delete
Else
'Cancel
End If

'Cleanup
Set oDestination = Nothing
Set oNS = Nothing
Set oApp = Nothing
Exit Sub
MyError:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbInformation, "SentItemsAdd_ItemAdd"
End Sub

Now to someday make this into an Add-in for portability... :rolleyes:

jwsnl
Jan 2nd, 2008, 03:03 AM
Hi all,

Nice forum; it must be heaven for those understanding VB.
I don't understand it and I'm not a programmer, but I do have a challange and that brought me here. So please be patient with me, I'll try to do my best.
Normally I find my way around with common sense, try & error butthis time I could need some help.

I picked this old posting as it matched closest what I found to my chalange. I'm using outlook 2003!

What I try to achive:
1) manage my in and outgoing email by adding a category to it. I do this by looking for specific codes and words in subject and body.
2) incoming mail I can move using the standard rules in outlook 2003 (tagging them to a category and move them to a specific folder)
3) outgoing mail I would like to keep in specific folders in my local PST file. Using rules like at 2) I can tag the email to a category.
But how can I move them???

I tried some with the code below.
A) My first problem: If I hit ALT+F11 I get a VB editor but how and were to add the code?
All what I see is: Project1 - Microsoft Office Outlook -- This outlook session.
The right screen is grey. I can add a userform or module or class module :sick: Which one??
B) This code is written for Outlook 2000, can I use it for 2003?
C) I tried some things with pasting the code in VB editor but this caused me scary errors in the Outlook client, which is vital for me so I stopped experimenting with it.

Thank for you replies.

Jan-Willem

ewall
Jan 7th, 2008, 01:46 PM
Hi, jwsnl--

The code listed above should work fine in Outlook 2000, 2003 or 2007 (I have tested it on all of these). It is pretty much as simple as pasting everything into the ThisOutlookSession window, then closing and re-opening Outlook.

However (!), taking a closer look, I see that it is using the MAPI profile stuff--so I bet it's expecting to find the Exchange server. I can't remember if I've tried the code on a non-MAPI profile, but it doesn't seem like it would work right.

Are you using local .PST files (POP3 email) only? That might be it... :(

jwsnl
Jan 7th, 2008, 11:44 PM
Hi Ewall,

Thanks for your replay after such a long time for your previous post.

We do use a exchange server but the policies in our companie ristrict storage on that server to 100MB so we are forced to use local PST files to keep some history on email.
I must admit that my original plan is to move the sent item to a local PST file.

Perhaps you could give me some guidance about how to install the code? It's not unthinkable I did something wrong there.

ewall
Jan 15th, 2008, 04:41 PM
Like I said... I wondering whether it will work at all, even if installed correctly. But here's the installation steps anyway:

Open Outlook
Press ALT-F11 to open the Visual Basic window
Double-click the "ThisOutlookSession" in the left-hand pane to open a (hopefully empty) window
Paste the code in (you will see that VB automagically puts in dividers etc.--just leave it alone
Close the VisualBasic windows--save the changes if prompted
Close all Outlook windows
Re-open Outlook and give it a test

If it doesn't work and you get errors... Just open up the VB window again and delete all the code you put in; next time you open Outlook the errors will be gone.

eatinmanna
Feb 25th, 2009, 01:48 PM
The original code looks very similar to something I'm trying to do, but have very limited VB experience.

I have an additional exchange mailbox added under my primary exchange mailbox so I can send/receive from this additional shared account. When I send an email from the added/secondary mailbox, it is placed in the "sent items" folder of the primary mailbox. I just want to create a custom rule to move messages sent from the secondary account to the secondary "sent items" folder.

Thoughts?

probev
Apr 10th, 2009, 06:05 PM
Guys, thanks for posting the code. I have been playing with it to customize it for my needs.

Without being a good VB developer...(I should say I am not developer at all), I have a quick question. I see there is a declaration for ItemSend...What was the reason to use "MAPILogonComplete"?

My question comes from the point that I am trying to capture the item and send it to folder "ABC" even before it is copied to the default "Sent Items" folder. Here is the reason if I manually move an item from foldaer DEF to the Sent Items folder, it automatically triggers the event and I get the question for moving the message.

In other words, is there a way to capture the item even before it is copied to the sent items folder and instead of "scanning" the default Sent Items, just get ItemSend and send the item to the folder....I am just thinking out loud...:D

Thanks a lot!!!

hockey_dave
Apr 29th, 2009, 12:45 PM
I added in a custom vb using the alt-F11 instructions above, but when I open Tools->Rules & Alerts->Change Rule->Edit Rule Settings->perform a custom action.
My new vba sub doesn't appear there. It's empty.

Thanks.

RobDog888
May 7th, 2009, 01:07 AM
Guys, thanks for posting the code. I have been playing with it to customize it for my needs.

Without being a good VB developer...(I should say I am not developer at all), I have a quick question. I see there is a declaration for ItemSend...What was the reason to use "MAPILogonComplete"?

My question comes from the point that I am trying to capture the item and send it to folder "ABC" even before it is copied to the default "Sent Items" folder. Here is the reason if I manually move an item from foldaer DEF to the Sent Items folder, it automatically triggers the event and I get the question for moving the message.

In other words, is there a way to capture the item even before it is copied to the sent items folder and instead of "scanning" the default Sent Items, just get ItemSend and send the item to the folder....I am just thinking out loud...:D

Thanks a lot!!!
Welcome to the Forums.

MAPILogonComplete is used to initialize the withevents declarations event.

To capture anything before the object is added to the sentitems folder you may try checking out the ItemSend event of the Application Object.

RobDog888
May 7th, 2009, 01:19 AM
The original code looks very similar to something I'm trying to do, but have very limited VB experience.

I have an additional exchange mailbox added under my primary exchange mailbox so I can send/receive from this additional shared account. When I send an email from the added/secondary mailbox, it is placed in the "sent items" folder of the primary mailbox. I just want to create a custom rule to move messages sent from the secondary account to the secondary "sent items" folder.

Thoughts?
Also see the ItemSend event. You can copy the item to the sent folder of your secondary sentitems folder and then watch the primary sentitems folder for the default one to show up. When it appears you can delete it since you will already have a copy in your seconday sentitmes folder.

RobDog888
May 7th, 2009, 01:20 AM
I added in a custom vb using the alt-F11 instructions above, but when I open Tools->Rules & Alerts->Change Rule->Edit Rule Settings->perform a custom action.
My new vba sub doesn't appear there. It's empty.

Thanks.
"Custom vb"? You should be using hte ThisOutlookSession classhi

paraguayanto
Jul 22nd, 2009, 06:34 AM
I think I may be missing the last step here:

I've pasted the code into the ThisOutlookSession and restarted Outlook (2003) but when I send a mail nothing happens.

Am I supposed to set a rule to trigger this code (if so, how?) or have I missed something incredibly stupid?!

Thanks.

paraguayanto
Jul 22nd, 2009, 07:49 AM
Well for reasons that make no sense to me whatsoever, the code has suddenly started working.

Great stuff. Thank you!

Pepperoni
Aug 13th, 2010, 12:27 PM
I have not been as lucky... I have added the code to the ThisSessionOnly field but did not see a macro or custom action listed. I may have messed the whole thing up by creating a macro, which added Module1 under Project1 at the same level as ThisSessionOnly. Now I can't delete Module1 or Project1. How do I start over? :sick:

Thanks much!

baja_yu
Aug 13th, 2010, 01:24 PM
Welcome to the forum. :)

A general practice for posting here is that you create a new, and your own thread when you have a question, instead of bumping old posts (like this one, over a year old) or asking in threads started by other people (also called hijacking). Instead create a new thread for your question and post a link to a relevant thread if you need.

koolsid
Aug 13th, 2010, 03:17 PM
Welcome to the forums Pepperoni :wave:

See if this link helps you...

http://www.vbforums.com/showthread.php?t=603677&highlight=Outlook