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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
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
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
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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
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

  • UDF Outlook Macro.txt
    1.4 KB · Views: 551

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
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

  • GetContact.txt
    626 bytes · Views: 615
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
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
M add new attendee to existing meetings with VBA Outlook VBA and Custom Forms 5
D VBA code to select a signature from the signatures list Outlook VBA and Custom Forms 3
D Create advanced search (email) via VBA with LONG QUERY (>1024 char) Outlook VBA and Custom Forms 2
David McKay VBA to manually forward using odd options Outlook VBA and Custom Forms 1
FryW Need help modifying a VBA script for in coming emails to auto set custom reminder time Outlook VBA and Custom Forms 0
S vba outlook search string with special characters Outlook VBA and Custom Forms 1
S VBA search string with special characters Outlook VBA and Custom Forms 1
U Outlook 2019 VBA run-time error 424 Outlook VBA and Custom Forms 2
DDB VBA to Auto Insert Date and Time in the signature Outlook VBA and Custom Forms 2
F VBA to move email from Non Default folder to Sub folders as per details given in excel file Outlook VBA and Custom Forms 11
G VBA to save selected Outlook msg with new name in selected network Windows folder Outlook VBA and Custom Forms 1
F Excel VBA to move mails for outlook 365 on secondary mail account Outlook VBA and Custom Forms 1
B Zoom automatically next email item (VBA) Outlook VBA and Custom Forms 2
T vba extract data from msg file as attachment file of mail message Outlook VBA and Custom Forms 1
K Outlook Office 365 VBA download attachment Outlook VBA and Custom Forms 2
A VBA Script - Print Date between first email in Category X and last email in Category Y Outlook VBA and Custom Forms 3
N Help creating a VBA macro with conditional formatting to change the font color of all external emails to red Outlook VBA and Custom Forms 5
N Save selected messages VBA does not save replies and/or messages that contain : in subject Outlook VBA and Custom Forms 1
Y Filter unread emails in a search folder vba help Outlook VBA and Custom Forms 0
V vBA for searching a cell's contents in Outlook and retrieving the subject line Outlook VBA and Custom Forms 1
B vBA for exporting excel file from outlook 2016 Outlook VBA and Custom Forms 3
L Modifying VBA script to delay running macro Outlook VBA and Custom Forms 3
L Need help modifying a VBA script for emails stuck in Outbox Outlook VBA and Custom Forms 6
K can't get custom form to update multiple contacts using VBA Outlook VBA and Custom Forms 3
S Excel vba code to manage outlook web app Using Outlook 10
H Custom Outlook Contact Form VBA Outlook VBA and Custom Forms 1
S Problem Checking the available stores in my Inbox (Outlook VBA) Outlook VBA and Custom Forms 0
S Outlook VBA How to adapt this code for using in a different Mail Inbox Outlook VBA and Custom Forms 0
S Add VBA save code Using Outlook 0
C Auto Run VBA Code on new email Outlook VBA and Custom Forms 1
O VBA Cases with Listbox - Can you use Multi-Select? Outlook VBA and Custom Forms 4
O VBA Outlook Message Attachment - Array Index Out of Bounds Outlook VBA and Custom Forms 0
V Modifying the built in forms with VBA Outlook VBA and Custom Forms 4
S Excel VBA and shared calendar issue Outlook VBA and Custom Forms 3
L Macro/VBA to Reply All, with the original attachments Outlook VBA and Custom Forms 3
L VBA unknown character Outlook VBA and Custom Forms 2
G Move tasks up/down todo list by VBA Outlook VBA and Custom Forms 1
diver864 vba for a rule to automatically accept meeting requests with 'vacation' in subject, change to all-day event, change to free, don't send reply Outlook VBA and Custom Forms 1
K Use VBA to find Sender and Recipient from Microsfot 365 Journaled Email Items Outlook VBA and Custom Forms 3
J Want to learn VBA Macros for Outlook. What book can you recommend? Outlook VBA and Custom Forms 2
F VBA code to dock Styles whenever I write or edit an email Outlook VBA and Custom Forms 0
C VBA to prompt for Sent folder destination Outlook VBA and Custom Forms 3
B Adding signature to bottom of VBA reply email Outlook VBA and Custom Forms 1
B Change Font and Font size using VBA Outlook VBA and Custom Forms 9

Similar threads

Top