Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook Rule to move mail-item to PST and change subject

Status
Not open for further replies.

ShekharBorker

Programmer
Aug 6, 2012
11
GB
I am facing a prblem when trying to set up an Outlook rule that changes the subject of the mail-item and moves it to a PST folder.

The rule that I have created is as follows:

Apply this rule after the message arrives
with 'EXADXX' in the message header
and on this machine only
move it to Archive folder
and run Project1.ThisOutlookSession.RptManage

The RptManage is as follows

Code:
Sub RptManage(MItem As Outlook.MailItem)
    ODATE = Mid("" & MItem.ReceivedTime, 7, 4) & Mid("" & MItem.ReceivedTime, 7, 4)
    MItem.Subject = " ODAT: " & ODATE & " " & MItem.Subject
    MItem.Save
End Sub

However, the mail-item is moved to the Archive folder without changes in the subject line. I have tried the following things:

1. I removed the Move To Folder action from the rule. In this case, the subject is changed.

2. I created two rules one for changing the subject and the other for moving it to the PST. I also ordered the rules to have the subject rule before the folder rule. The result is that the mail-item is moved to Archive folder without any changes to the subject.

3. I have tried removing the Move to Folder action from the rule and using the mail-item "Move" method within the macro as follows:
Code:
Sub RptManage(MItem As Outlook.MailItem)
    Dim ns As NameSpace
    Dim ArchFolder As MAPIFolder
    ODATE = Mid("" & MItem.ReceivedTime, 7, 4) & Mid("" & MItem.ReceivedTime, 7, 4)
    MItem.Subject = " ODAT: " & ODATE & " " & MItem.Subject
    MItem.Save
    Set ns = Application.GetNamespace("MAPI")
    ArchFolder = ns.Folders.Item("Archive")
    Mitem.Move(ArchFolder)
End Sub

When the rule executed the subject had changed but the mail-item hadn't moved to the Archive folder.

Could you please confirm whether my requirement can be met using Rules + VBA Macros?

Also, I am not sure why I am getting the results. Any help on this is much appreciated.

Thanks
Shekhar Borker
 
hi,

reason is, your procedure has an MItem argument that needs to be supplied when it is called.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Did you debug your procedure to be sure that it does what you intend?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Hi Skip,

Yes. The results are as explained in my first post.

Thanks,
Shekhar Borker.
 
>but the mail-item hadn't moved to the Archive folder.

Are you sure that

ArchFolder = ns.Folders.Item("Archive")

is getting the folder that you think it is?
 
Thanks Strongm for pointing out the mistake!

It needs to be Set ArchFolder = ns.Folders.Item("Archive")

 
I have corrected the mistake that strongm has pointed out above and changed the code as follows:

Code:
Sub RptManage(MItem As Outlook.MailItem)
    Dim ns As NameSpace
    Dim ArchFolder As MAPIFolder
    Dim ODATE As String
    ODATE = Mid("" & MItem.ReceivedTime, 7, 4) & Mid("" & MItem.ReceivedTime, 7, 4)
    MItem.Subject = " ODAT: " & ODATE & " " & MItem.Subject
    MItem.Save
    Set ns = Application.GetNamespace("MAPI")
    Set ArchFolder = ns.Folders.Item("Archive")
    Mitem.Move(ArchFolder)
End Sub

Everything seems to work fine until the move. However, the Move method isn't executing correctly for some reason.

Thanks,
Shekhar Borker
 
CAn you try removing the brackets?

Mitem.Move(ArchFolder) => Mitem.Move ArchFolder
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top