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
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
 
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
 
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
 
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.
 
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
 
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.
 
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

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
 
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 --
 
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.
Similar threads
Thread starter Title Forum Replies Date
J VBA Cannot programmatically input or change Value for User Defined field Using Outlook 1
Z Import Tasks from Access Using VBA including User Defined Fields Outlook VBA and Custom Forms 0
B Outlook Business Contact Manager with SQL to Excel, User Defined Fields in BCM don't sync in SQL. Can I use VBA code to copy 1 field to another? BCM (Business Contact Manager) 0
M Multiple User Defined Fields that can be added, changed, updated using VBA and user form Outlook VBA and Custom Forms 0
Wotme Syntax for user defined field in VBA Using Outlook 1
I shared mailbox - can i create a rule (vba) for every user? Outlook VBA and Custom Forms 1
S Ask user to input email template through VBA Outlook VBA and Custom Forms 1
R vba instantiated internetExplorer.application differs from user la Outlook VBA and Custom Forms 1
efire9207 VBA Outlook Contacts Outlook VBA and Custom Forms 6
B Requesting VBA code to make Outlook prompt for confirmation when deleting a task? Outlook VBA and Custom Forms 4
M Outlook 365 VBA Auto-Forward Only the first of Duplicate Emails Outlook VBA and Custom Forms 2
N VBA Code Not Working correctly Outlook VBA and Custom Forms 1
L VBA to Triage Incoming Email Outlook VBA and Custom Forms 0
J Outlook VBA to send from Non-default Account & Data Files Outlook VBA and Custom Forms 4
H using VBA to edit subject line Outlook VBA and Custom Forms 0
G Get current open draft message body from VBA Outlook VBA and Custom Forms 1
P VBA to add email address to Outlook 365 rule Outlook VBA and Custom Forms 0
M Outlook 2016 outlook vba to look into shared mailbox Outlook VBA and Custom Forms 0
V VBA Categories unrelated to visible calendar and Visual appointment Categories Outlook VBA and Custom Forms 2
D Outlook VBA forward the selected email to the original sender’s email ID (including the email used in TO, CC Field) from the email chain Outlook VBA and Custom Forms 2
R Outlook 365 VBA AUTO SEND WITH DELAY FOR EACH EMAIL Outlook VBA and Custom Forms 0
R Outlook 2019 VBA to List Meetings in Rooms Outlook VBA and Custom Forms 0
geoffnoakes Counting and/or listing fired reminders via VBA Using Outlook 1
O VBA - Regex - remove double line spacing Outlook VBA and Custom Forms 1
D.Moore Strange VBA error Outlook VBA and Custom Forms 4
B Modify VBA to create a RULE to block multiple messages Outlook VBA and Custom Forms 0
D Outlook 2021 Using vba code to delete all my spamfolders not only the default one. Outlook VBA and Custom Forms 0
K vba code to auto download email into a specific folder in local hard disk as and when any new email arrives in Inbox/subfolder Outlook VBA and Custom Forms 0
D VBA - unable to set rule condition 'on this computer only' Outlook VBA and Custom Forms 5
L Fetch, edit and forward an email with VBA outlook Outlook VBA and Custom Forms 2
BartH VBA no longer working in Outlook Outlook VBA and Custom Forms 1
W Can vba(for outlook) do these 2 things or not? Outlook VBA and Custom Forms 2
MattC Changing the font of an email with VBA Outlook VBA and Custom Forms 1
P MailItem.To Property with VBA not work Outlook VBA and Custom Forms 2
P Tweak vba so it can target another mailbox Outlook VBA and Custom Forms 1
A Outlook 2010 VBA fails to launch Outlook VBA and Custom Forms 2
richardwing Outlook 365 VBA to access "Other Actions" menu for incoming emails in outlook Outlook VBA and Custom Forms 0
W Create a Quick Step or VBA to SAVE AS PDF in G:|Data|Client File Outlook VBA and Custom Forms 1
J Outlook Rules VBA Run a Script - Multiple Rules Outlook VBA and Custom Forms 0
C Outlook (desktop app for Microsoft365) restarts every time I save my VBA? Using Outlook 1
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
TedSch Small vba to kill political email Outlook VBA and Custom Forms 3
E Outlook 365 Outlook/VBA Outlook VBA and Custom Forms 11
N VBA Macro To Save Emails Outlook VBA and Custom Forms 1
Z VBA Forward vs manual forward Outlook VBA and Custom Forms 2
J VBA for outlook to compare and sync between calendar Outlook VBA and Custom Forms 1
A Any way to force sort by/group by on search results with VBA? Outlook VBA and Custom Forms 1
E Default shape via VBA Outlook VBA and Custom Forms 4
A Change settings Send/receive VBA Outlook VBA and Custom Forms 0
E Outlook VBA change GetDefaultFolder dynamically Outlook VBA and Custom Forms 6

Similar threads

Back
Top