none
Custom integer field to message not quite working RRS feed

  • Question

  • I created a new view and added a custom numeric field called ID.  The user can key values in w/o an issue.

    I'm now trying to set the value through VBA with some code like:

          message.UserProperties.Find("ID").Value = 555

    If i prepopulate the field by hand with a 0 first, this works however, if i don't put in a number first by hand, the code does not change the field - which stays blank.   There doesn't seem to be a way to set a default value for the field.

    Any idea what is going on and how to get around it?


    ajw

    Sunday, July 19, 2015 12:47 PM

All replies

  • Hello ajw,

    Try to refresh the view to get your changes visible. Use the CurrentFolder method of the Explorer class which allows to get or set a Folder object that represents the current folder displayed in the explorer. 

    Sub ChangeCurrentFolder()  
     Dim myNamespace As Outlook.NameSpace 
     Set myNamespace = Application.GetNamespace("MAPI")  
     Set Application.ActiveExplorer.CurrentFolder = _  
     myNamespace.GetDefaultFolder(olFolderCalendar)  
    End Sub

    So, you can change the view for a moment and then set it back. Thus, the view will be refreshed.

    Sunday, July 19, 2015 1:26 PM
  • I'm not sure it's about the view.  if I set it in the code, change views, then come back - it's still blank.   If I set it to 0 by hand and run the program, you see the new number immediately.

    I'll give the refresh a shot though.  Thanks for the reply!  :)


    ajw

    Sunday, July 19, 2015 1:31 PM
  • Hi Geek,

    Do you do a Save on the MailItem after you modify the ID custom property?
    Sunday, July 19, 2015 2:46 PM
  • Of course, if the property does already exist, UserProperties.Find will return null. Use UserProperties.Add in that case:

    set prop = message.UserProperties.Find("ID")
    if (prop Is Nothing) Then 
      set prop = message.UserProperties.Add("ID", ...)
    End If
    prop.Value = 555


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Sunday, July 19, 2015 6:18 PM
  •       message.UserProperties.Find("ID").Value = 555


    Dmitry,

    Wouldn't this code throw an exception if the "ID" property hadn't already been created somewhere else ( either in other code or through view in-cell editing) ?

    Sunday, July 19, 2015 6:59 PM
  • It would unless you have "on error resume next". Try to add the following for debugging purposes:

    MsgBox TypeName(message.UserProperties.Find("ID"))


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Sunday, July 19, 2015 7:02 PM
  • I understand your point.  It's too bad we can't see all of the OP's VBA code
    Sunday, July 19, 2015 7:06 PM