none
move data into custom fields with VBA in Outlook calendar RRS feed

  • Question

  • Hello,

    I am looking to convert fields that are already populated like my subject field in my outlook calendar and to move that data into a custom field I have created. I have found code that does this for contacts but I am pretty new to VBA and am having trouble getting it to work with the calendar.

    Any ideas would be helpful. I am working in Outlook 2013 and here is the code that works for contacts, but I would like to use it for the calendar to move data into my custom fields without having to do it one by one. Right now it errors out at  objItem.UserProperties("Custom1") = objItem.User1. I assume it is because userproperties does not make sense with reference a custom form in calendar but I am not sure what to replace it with. 

    Sub ConvertFields()
      Dim objApp As Application
      Dim objNS As NameSpace
      Dim objFolder As MAPIFolder
      Dim objItems As Items
      Dim objItem As Object

      Set objApp = CreateObject("Outlook.Application")
      Set objNS = objApp.GetNamespace("MAPI")
      Set objFolder = objNS.PickFolder
      If Not objFolder Is Nothing Then
        Set objItems = objFolder.Items
        For Each objItem In objItems
          ' make sure you have a Contact item
          If objItem.Class = olContact Then
            ' convert to your published custom form
            objItem.MessageClass = "IPM.Contact.Custom"
            ' copy data to your custom fields
            objItem.UserProperties("Custom1") = objItem.User1
            objItem.UserProperties("Custom2") = objItem.User2
            objItem.UserProperties("Custom3") = objItem.User3
            objItem.UserProperties("Custom4") = objItem.User4
            objItem.User1 = ""
            objItem.User2 = ""
            objItem.User3 = ""
            objItem.User4 = ""
            objItem.Save
          End If
        Next
      End If

      Set objItems = Nothing
      Set objItem = Nothing
      Set objFolder = Nothing
      Set objNS = Nothing
      Set objApp = Nothing
    End Sub

    Wednesday, April 3, 2013 2:25 PM

Answers

  • If this is supposed to run in the Outlook VBA project do not create a new Outlook Application object, use the intrinsic and safe Application object provided by the VBA project.
     
    User1 - User4 are properties that are provided for Contacts, they don't exist for appointments. In addition, if an item doesn't have a specific UserProperty you must add it to that collection. The code you're using also is only for handling custom contact items ("IPM.Contact.Custom"), not standard ones.
    Try code something like this to add the text "Test" to a new UserProperty named "Custom1":
     
    Sub ConvertFields()
      Dim objApp As Application
      Dim objNS As NameSpace
      Dim objFolder As MAPIFolder
      Dim objItems As Items
      Dim objItem As Object
      Dim objAppt As AppointmentItem
      Dim objProp As Outlook.UserProperty
     
      Set objApp = Application
      Set objNS = objApp.GetNamespace("MAPI")
      Set objFolder = objNS.PickFolder
      If Not objFolder Is Nothing Then
        Set objItems = objFolder.Items
        For Each objItem In objItems
          ' make sure you have a Appointment item
          If objItem.Class = olAppointment Then
            ' copy data to a new custom field
            Set objAppt = objItem
            Set objProp = objAppt.UserProperties.Add("Custom1", olText, True)
            objProp.Value = "Test"
            objAppt.Save
          End If
        Next
      End If
     
      Set objItems = Nothing
      Set objItem = Nothing
      Set objAppt = Nothing
      Set objFolder = Nothing
      Set objNS = Nothing
      Set objApp = Nothing
    End Sub

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "LIZH111" <=?utf-8?B?TElaSDExMQ==?=> wrote in message news:c932d016-f6e1-4b10-a9e6-a49886605235...

    Hello,

    I am looking to convert fields that are already populated like my subject field in my outlook calendar and to move that data into a custom field I have created. I have found code that does this for contacts but I am pretty new to VBA and am having trouble getting it to work with the calendar.

    Any ideas would be helpful. I am working in Outlook 2013 and here is the code that works for contacts, but I would like to use it for the calendar to move data into my custom fields without having to do it one by one. Right now it errors out at  objItem.UserProperties("Custom1") = objItem.User1. I assume it is because userproperties does not make sense with reference a custom form in calendar but I am not sure what to replace it with. 

    Sub ConvertFields()
      Dim objApp As Application
      Dim objNS As NameSpace
      Dim objFolder As MAPIFolder
      Dim objItems As Items
      Dim objItem As Object

      Set objApp = CreateObject("Outlook.Application")
      Set objNS = objApp.GetNamespace("MAPI")
      Set objFolder = objNS.PickFolder
      If Not objFolder Is Nothing Then
        Set objItems = objFolder.Items
        For Each objItem In objItems
          ' make sure you have a Contact item
          If objItem.Class = olContact Then
            ' convert to your published custom form
            objItem.MessageClass = "IPM.Contact.Custom"
            ' copy data to your custom fields
            objItem.UserProperties("Custom1") = objItem.User1
            objItem.UserProperties("Custom2") = objItem.User2
            objItem.UserProperties("Custom3") = objItem.User3
            objItem.UserProperties("Custom4") = objItem.User4
            objItem.User1 = ""
            objItem.User2 = ""
            objItem.User3 = ""
            objItem.User4 = ""
            objItem.Save
          End If
        Next
      End If

      Set objItems = Nothing
      Set objItem = Nothing
      Set objFolder = Nothing
      Set objNS = Nothing
      Set objApp = Nothing
    End Sub


    Ken Slovak MVP - Outlook
    • Marked as answer by LIZH111 Wednesday, April 3, 2013 7:16 PM
    Wednesday, April 3, 2013 3:26 PM
    Moderator

All replies

  • If this is supposed to run in the Outlook VBA project do not create a new Outlook Application object, use the intrinsic and safe Application object provided by the VBA project.
     
    User1 - User4 are properties that are provided for Contacts, they don't exist for appointments. In addition, if an item doesn't have a specific UserProperty you must add it to that collection. The code you're using also is only for handling custom contact items ("IPM.Contact.Custom"), not standard ones.
    Try code something like this to add the text "Test" to a new UserProperty named "Custom1":
     
    Sub ConvertFields()
      Dim objApp As Application
      Dim objNS As NameSpace
      Dim objFolder As MAPIFolder
      Dim objItems As Items
      Dim objItem As Object
      Dim objAppt As AppointmentItem
      Dim objProp As Outlook.UserProperty
     
      Set objApp = Application
      Set objNS = objApp.GetNamespace("MAPI")
      Set objFolder = objNS.PickFolder
      If Not objFolder Is Nothing Then
        Set objItems = objFolder.Items
        For Each objItem In objItems
          ' make sure you have a Appointment item
          If objItem.Class = olAppointment Then
            ' copy data to a new custom field
            Set objAppt = objItem
            Set objProp = objAppt.UserProperties.Add("Custom1", olText, True)
            objProp.Value = "Test"
            objAppt.Save
          End If
        Next
      End If
     
      Set objItems = Nothing
      Set objItem = Nothing
      Set objAppt = Nothing
      Set objFolder = Nothing
      Set objNS = Nothing
      Set objApp = Nothing
    End Sub

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "LIZH111" <=?utf-8?B?TElaSDExMQ==?=> wrote in message news:c932d016-f6e1-4b10-a9e6-a49886605235...

    Hello,

    I am looking to convert fields that are already populated like my subject field in my outlook calendar and to move that data into a custom field I have created. I have found code that does this for contacts but I am pretty new to VBA and am having trouble getting it to work with the calendar.

    Any ideas would be helpful. I am working in Outlook 2013 and here is the code that works for contacts, but I would like to use it for the calendar to move data into my custom fields without having to do it one by one. Right now it errors out at  objItem.UserProperties("Custom1") = objItem.User1. I assume it is because userproperties does not make sense with reference a custom form in calendar but I am not sure what to replace it with. 

    Sub ConvertFields()
      Dim objApp As Application
      Dim objNS As NameSpace
      Dim objFolder As MAPIFolder
      Dim objItems As Items
      Dim objItem As Object

      Set objApp = CreateObject("Outlook.Application")
      Set objNS = objApp.GetNamespace("MAPI")
      Set objFolder = objNS.PickFolder
      If Not objFolder Is Nothing Then
        Set objItems = objFolder.Items
        For Each objItem In objItems
          ' make sure you have a Contact item
          If objItem.Class = olContact Then
            ' convert to your published custom form
            objItem.MessageClass = "IPM.Contact.Custom"
            ' copy data to your custom fields
            objItem.UserProperties("Custom1") = objItem.User1
            objItem.UserProperties("Custom2") = objItem.User2
            objItem.UserProperties("Custom3") = objItem.User3
            objItem.UserProperties("Custom4") = objItem.User4
            objItem.User1 = ""
            objItem.User2 = ""
            objItem.User3 = ""
            objItem.User4 = ""
            objItem.Save
          End If
        Next
      End If

      Set objItems = Nothing
      Set objItem = Nothing
      Set objFolder = Nothing
      Set objNS = Nothing
      Set objApp = Nothing
    End Sub


    Ken Slovak MVP - Outlook
    • Marked as answer by LIZH111 Wednesday, April 3, 2013 7:16 PM
    Wednesday, April 3, 2013 3:26 PM
    Moderator
  • Thanks for your time and help Ken. This gets me on the road for what I am trying to work out. Have a Great day!
    Wednesday, April 3, 2013 7:16 PM