VBA with User Defined fields

Status
Not open for further replies.

Shadaab

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#1
Dear Experts,

I'm using text 'user defined fields' to categorize my inbox, I need to setup rules (script based) on these UDFs so that an email from a specific group of people will automatically have the UDF set,

for example an email from a certain group A will have UDF set as Project1 and for another group B of senders the UDF will set to Project2, today I do this manually and this is taking me some time especially with outlook 2016 where the search doesn't consider UDFs.

I have used VBA programming with excel but outlook VBA syntax seems a lot different, any help with VBA code appreciated.

rgds
SQ
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#2
I have a macro that looks up a contact and categorizes the email based on the contact's category - Assign Categories to Messages using Contact Category - that should be a start. Replace the category with your custom field. If you don't need to look up the contact and are using multiple rules to set the udf, you would use a simpler run a script rule - Outlook's Rules and Alerts: Run a Script

Code:
Public Sub ShowMessage(Item As Outlook.MailItem)
Dim objProp As Outlook.UserProperty
Set objProp = Item.UserProperties.Add("field name", olText, True)
 objProp.Value = "value"
 item.Save
End Sub
 

Shadaab

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#3
Hi Diane,

thanks for your prompt reply, I like the idea of contact categories to define UDF.

I need some help here, how do I replace the Category with UDF?

I have UDF called 'Action' and when I get a mail from group A sender, the UDF 'Action' field should change to 'Project1'

can you help with the syntax?

rgds
SQ
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#4
How are you defining who is in Group A?

in the link to the category code, you'd replace this:
Item.Categories = oContact.Categories

with this:

Set objProp = Item.UserProperties.Add("Action", olText, True)
objProp.Value = "Project1"

But... you need a way to identify who is in Group A. If you are using rules, you can use the Run a Script action and use the script i posted above.
 

Shadaab

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#5
Hi Diane,

Initially I thought of running a script for a group of users via 'Rules & Alerts', and then create multiple Rules to allocate each Action i.e. Project 1, 2...

but now I'd like to try using your idea of contact category to identify and assign Project to the Action UDF, with a single Rule script run as soon as I receive a mail. (unsure if the Contact Category will copy over to the UDF as text)

Now I'm stuck again, I've put the code in outlook but I don't see the Macro under Rules and Alerts as indicated in your mail.

Also I am scared to run the test script on all these hundreds of mails, if it doesn't work all the UDFs would be disturbed and it will take me a long time to correct them, is it possible that I can test this on a test folder instead of the inbox.

thanks again for your valuable help

best regards
SQ
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#6
If you don't see it, it's because you can't run it manually. The first macro on the page is an item add macro - it only runs when a new message is added to the folder. There is a stub in the sample code that calls it manually on the selected message - because you are adding categories, undoing it is fairly simple, just remove the category. (run a scripts are only visible to rules.)

Create a subfolder under the inbox called Test then change the line in the appt startup folder to
Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Folders("Test").Items
Click in the app startup macro and click run to trigger it then move or copy messages into the test folder.
 

Shadaab

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#7
Hi Diane,

I'm not getting anywhere, I'm not sure what I'd doing wrong here.
I have put all the code on the outlook session, I get the Show Message macro on the Outlook rule as a script for which I have set a rule to run when I receive a mail from myself, also as suggested I've put objProp.Value for the UDF "Action" to be set as "Project 1", but it doesn't work, have tried quite a few combinations now,

I'm attaching the code in a txt file,

best regards
SQ
 

Attachments

Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#8
you have two different solutions there - one is an itemadd macro that looks up the category on a contact and sets that category on a message. It runs as messages arrive. The other one is a run a script rule that sets a custom field when a message matching the rule arrives. (The run a script run can be in a module. )

The run a script is working for me - TestScriptManual will run the script on the selected message so you can see if it works. If so, the problem is likely in the rule.

Code:
 'Use this to run the macro on a message already in the Inbox
Public Sub TestScriptManual()
    Dim objMail As Object
    Set objMail = Application.ActiveExplorer.Selection.Item(1)
    ShowMessage objMail
Set objMail = Nothing
End Sub



' run a script
Public Sub ShowMessage(Item As Outlook.MailItem)
Dim objProp As Outlook.UserProperty
Set objProp = Item.UserProperties.Add("Action", olText, True)
objProp.Value = "Project 1"
Item.Save
End Sub
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#9
if you wanted to use the item add rule instead and set the custom field based on the category of the contact, you'd use this snippet in the itemadd macro instead of the code that sets the category. This code runs on all messages though (but it can be turned into a run a script rule.)

Code:
-- snip ---
     If Not oContact Is Nothing Then

            Dim objProp As Outlook.UserProperty
            Set objProp = Item.UserProperties.Add("Action", olText, True)
            objProp.Value = oContact.Categories ' "Project 1"
             Item.Save
                
                 'Item.Categories = oContact.Categories
                 Set oContact = Nothing
                         
            Set Item = Nothing
             End If

--snip --
 

Shadaab

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#10
Hi Diane,

thanks for your guidance, I'm now able to set UDFs based on rules, and it works like a charm.

unfortunately I cannot get the contact categories to set the UDF with the modified code, on debugging this code for a new message this below piece doesn't fetch any contact details even though a contact is created in my address book with a category, the sender information is set but oContact remains as Nothing

Set oContact = oSender.GetContact

find attached code, changed as per your instructions

rgds
SQ
 

Attachments

Status
Not open for further replies.
Top