?
Solved

Help on modify a VBA Macro to run on selected InBox items in Outlook 2007

Posted on 2011-10-26
10
Medium Priority
?
537 Views
Last Modified: 2012-05-12
With the assistance of EE, we have a VBA that runs excellently on Outlook 2007.  This VBA changes the contents of a User-Defined column within the Inbox that has been selected, but it only works only for one or the first selected.

What we need is to have the Macro run on ALL selected Inbox Item.  

Please review the code attached to see where we can modify to have run on all selected items.
0
Comment
Question by:rayluvs
  • 6
  • 4
10 Comments
 

Author Comment

by:rayluvs
ID: 37034544
here the code
Sub FillInColumn()
Dim olkMsg As Outlook.MailItem, olkProp As Object
    For Each olkMsg In Application.ActiveExplorer.Selection
        'Set olkProp = olkMsg.UserProperties.Item("dd")
        If TypeName(olkProp) = "Nothing" Then
            Set olkProp = olkMsg.UserProperties.Add("Proyecto", olText, True)
            olkProp.Value = "PROJECTONE"
            olkMsg.Save
        End If
    Next
    Set olkMsg = Nothing
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:David Lee
ID: 37034972
Hi, Ramante.

I'm a little confused.  The code is already designed to run against all selected items.  Are you saying that it's not working when you select multiple items?
0
 

Author Comment

by:rayluvs
ID: 37035442
It didnt work.  When I select 4 items from a displayed 120, it just saves the first one.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

Author Comment

by:rayluvs
ID: 37035445
To be more specific, if it only saves on the first selected item, the rest it leaves as is.
0
 
LVL 76

Accepted Solution

by:
David Lee earned 2000 total points
ID: 37035575
I see the problem.  Try this version.
Sub FillInColumn()
    Dim olkMsg As Outlook.MailItem, olkProp As Object
    For Each olkMsg In Application.ActiveExplorer.Selection
        'Set olkProp = olkMsg.UserProperties.Item("dd")
        If TypeName(olkProp) = "Nothing" Then
            Set olkProp = olkMsg.UserProperties.Add("Proyecto", olText, True)
            olkProp.Value = "PROJECTONE"
            olkMsg.Save
        End If
        Set olkProp = Nothing
    Next
    Set olkMsg = Nothing
End Sub

Open in new window

0
 

Author Comment

by:rayluvs
ID: 37035931
Yes it worked perfectly!!!!

FYI: found one of your articles "Extending Outlook Rules via Scripting".  It helped us a lot.  Thanx!

Notice the change was "Set olkProp = Nothing", can u give brief explanation of this line?
0
 
LVL 76

Assisted Solution

by:David Lee
David Lee earned 2000 total points
ID: 37036802
You're welcome.

The command

    Set olkProp = Nothing

ensures that the variable olkProp is empty at the beginning of each loop.  The reason the loop was failing after the first pass was because this line

    If TypeName(olkProp) = "Nothing"

is checking to see if olkProp is empty.  After the first loop it wasn't empty, so the code inside the IF ... THEN wasn't firing.  Another solution (below) would be to remove the is to remove the test altogether, which then allows removal of setting the variable to "Nothing".  
Sub FillInColumn()
    Dim olkMsg As Outlook.MailItem, olkProp As Object
    For Each olkMsg In Application.ActiveExplorer.Selection
        Set olkProp = olkMsg.UserProperties.Add("Proyecto", olText, True)
        olkProp.Value = "PROJECTONE"
        olkMsg.Save
    Next
    Set olkMsg = Nothing
End Sub

Open in new window

0
 

Author Comment

by:rayluvs
ID: 37037747
Thanx!
0
 

Author Closing Comment

by:rayluvs
ID: 37037764
Thanx
0
 
LVL 76

Expert Comment

by:David Lee
ID: 37037887
You're welcome!
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
There can be many situations demanding the conversion of Outlook OST files to PST format and as such, there is no shortage of automated tools to perform this conversion. However, what makes Stellar OST to PST converter stand above the rest? Let us e…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question