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!

Need to Add a Userdefined Property to Mail Items... 1

Status
Not open for further replies.

jmrdaddy

Technical User
Jun 10, 2002
31
0
0
US
I need to add a user defined property to each mail item in a folder. The code below works without errors but it only adds the property to the currently selected mail item or the first mail item in a selected group of items. How can I make it work for every item in the folder? Thanks.

Code:
Option Explicit

Sub AddAUserDefinedProperty()

    Dim olApplication   As Outlook.Application
    Dim olNameSpace     As Outlook.NameSpace
    Dim olFolder        As Outlook.MAPIFolder
    Dim objItem         As Object
    Dim strDomain       As String
    Dim olProperty      As Outlook.UserProperty

    Set olApplication = New Outlook.Application
    Set olNameSpace = olApplication.GetNamespace("Mapi")
    Set olFolder = olNameSpace.GetDefaultFolder(olFolderJunk)

    For Each olItem In olFolder.Items

        strDomain = Mid(olItem.SenderEmailAddress, _
            InStr(1, olItem.SenderEmailAddress, "@") + 1)

        Set olProperty = olItem.UserProperties.Add("Domain", olText)

        olProperty.Value = strDomain

        Debug.Print olItem.SenderEmailAddress, olProperty.Value

    Next olItem

    Set olApplication = Nothing
    Set olNameSpace = Nothing
    Set olFolder = Nothing
    Set olProperty = Nothing

End Sub

 
i need to correct the use of the variable objItem where its used instead of olItem. after that what are your suggestions
 
Hi jmrdaddy,

For reference this as also posted at VBA Express where I have answered it.

The mailitem needs saving {olItem.Save) after the property has been set.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
You are so awesome! Thank you! Thank you! Thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top