none
Access a user-defined field from a task RRS feed

  • Question

  • I've been writing Excel macros for a long time and am quite proficient at it, but this is my first Outlook macro.

    I'm trying to access a user-defined field that I have added to my outlook task list.  I can see from the Show Columns dialog in Outlook that the field exists.  I have added the field as a column in both my Task and my To-Do Lists, and I can see it and its attendant values.  But, my code below is not finding the UserProperty for my field.

    The field name is "Rank", and that is what I've set mkstrColumnName to.  The function is supposed to walk through the To-Do List and for each item, perform some action on the value in its Rank field.  Unfortunately, it is asserting at the line:

    Set upCurrent = upsCurrent.Find(mkstrColumnName, True)

    So, it never gets to the point where it can take that action.

    I have even tried to create that field if the function fails to see it (the lines just before the first Exit For statement) and when I'm debugging, it seems to have created the new UserProperty, as upCurrent is not nothing and I can see "Rank" in its Name.  But when I rerun the macro, it still does not show up.

    I should mention that this function is defined inside a class. The user-invoked macro, written in another module, instantiates the class and then calls the AssignRank method.

    I'm sure it's something simple that I'm overlooking or that my limited knowledge of the workings of Outlook is preventing me from seeing, but after several hours of researching and experimenting, I'm still at an impasse.  Can you take a look at this code and tell me where I've gone wrong?  Thanks.

    Public Function AssignRanks() As Integer
        Const kstrMethodName = "AssignRanks"
    
        Dim bIsProperRank As Boolean
        Dim fldrTask As Outlook.Folder
        Dim objItem As Object
        Dim Session As Outlook.NameSpace
        Dim strImportance As String
        Dim strOrder As String
        Dim strRank As String
        Dim strTaskName As String
        Dim tiCurrent As Outlook.TaskItem
        Dim upCurrent As Outlook.UserProperty
        Dim upsCurrent As Outlook.UserProperties
    
        Set Session = Application.Session
        Set fldrTask = Session.GetDefaultFolder(olFolderToDo)
        For Each objItem In fldrTask.Items
            If (objItem.Class = olTask) Then
    #If Debugging Then
                Debug.Assert Not objItem Is Nothing
    #End If
                Set tiCurrent = objItem
                Set upsCurrent = tiCurrent.UserProperties
    #If Debugging Then
                Debug.Assert Not upsCurrent Is Nothing
    #End If
                Set upCurrent = upsCurrent.Find(mkstrColumnName, True)
    #If Debugging Then
                Debug.Assert Not upCurrent Is Nothing
    #End If
                If (upCurrent Is Nothing) Then
    #If Debugging Then
                    Debug.Assert False
    #End If
                    Set upCurrent = upsCurrent.Add(mkstrColumnName, olText, True)
                    ShowErrorMessage mkstrModuleName, kstrMethodName, "Field """ & mkstrColumnName & """ was not found in the task list, but has been added.  Restart Outlook, add the column to your view, set the ranks and then rerun the macro."
                    Exit For
                Else
    
                   ' Perform actions on task rank value here.
    
                End If
            End If
        Next
    End Function ' AssignRanks


    • Edited by harleygnuya Thursday, September 21, 2017 12:00 AM
    Wednesday, September 20, 2017 11:50 PM

Answers

All replies

  • After addin the property, finally you need to save the task item.

    Michael Bauer - MVP Outlook
    Category Manager - Easily Share your Color Categories

    • Marked as answer by harleygnuya Thursday, September 21, 2017 2:32 PM
    Thursday, September 21, 2017 5:35 AM
  • Hi harleygnuya,

    What do you mean that it is asserting at the line "Set upCurrent = upsCurrent.Find(mkstrColumnName, True)" ?

    If it means that the macro stops running, did you get any error message? In my test, even if there is no "Rank" column, the macro could still run.

    Besides, I did not see where mkstrColumnName was set as Rank, please check if it is empty.

    Excepts UserProperties.Find methoh, have you tried UserProperties.Item property?

    Such as

     Set upCurrent = upsCurrent.Item("Rank")

    Best Regards,

    Terry

    Thursday, September 21, 2017 10:42 AM
  • Thanks, Michael, for responding.

    That seemed to work.  I tested it by first deleting the UDF through Outlook's UI.  I then ran my macro and, as expected, it did not find the field and fell through to the code that created it.  I then checked, using the UI, and the field didn't show up in the list, but when I exited and restarted Outlook and checked again, it was there.  I then ran my macro again and, voila, it found the field!  So, now it's there and I thank you.

    That's great, but I'm still left with trying to understand why my macro did not find the field in the first place.  It showed up in the UI, but the macro couldn't find it.  Is there something different about a UDF defined in the UI, that a macro can't find it?

    I should say that in my efforts prior to posting my original post, I did walk the UserProperties list using the debugger and, sure enough, the field was just not there.  Why would that be?  Would a UDF defined in the UI not show up as a UserProperty?

    Thanks, Ken

    Thursday, September 21, 2017 2:06 PM
  • Thanks, Terry, for responding.  As you can see by my response to Michael, I got it to save the field and now it can find it.  I am still curious about the questions I asked him and would appreciate it if you could reply with any information or ideas you might have.

    Regarding your questions:

    I should have more properly stated that it asserted at the Debug.Assert statement immediately following the line I referenced.  I apologize for any confusion.

    One of the things I tried early on was to put an On Error statement just before the assignment line, but it never hit my error handler, so it was not generating an error.  The macro ran just fine, it just didn't find the UserProperty and returned Nothing.

    mkstrColumnName is a private constant defined at the module level.  Yes, the very first thing I did when debugging was to check and at the point where the line in question was about to execute, it did have the value "Rank".

    Among the many things I tried along the way, I did try UserProperties.Item and it didn't find the UserProperty either.  From what I am able to figure out, I think that, for some reason, the code was not seeing the "Rank" field, even though I could see it in the Outlook UI.  Why would that happen?

    Thanks again,

    Ken

    Thursday, September 21, 2017 2:19 PM
  • When using Outlook´s dialog to create a view, it adds the UDF to a folder. It doesn´t add it also to all items of the folder.

    By code you can add a UDF both to an item and to the folder if it doesn´t already exist in the folder. A field that only exists in the folder won´t be found by the code that looks at the item.


    Michael Bauer - MVP Outlook
    Category Manager - Easily Share your Color Categories


    Tuesday, September 26, 2017 6:01 AM