Create a contact in a non-default Outlook contact folder

前端 未结 1 872
长发绾君心
长发绾君心 2021-01-28 08:14

I would like to create a contact in a non-default Outlook contact folder with Excel VBA 2010.

In this example, the folder name is “azerty”, located in
\\mypersonna

1条回答
  •  一整个雨季
    2021-01-28 08:28

    If you need to create an Outlook item in the specific folder use the Add method of the Items class instead.

    Dim myolApp As Outlook.Application
    Dim myNamespace As Outlook.Namespace
    Set myolApp = CreateObject("Outlook.Application")
    Set myNamespace = myolApp.GetNamespace("MAPI")
    Set Folder = myNamespace.GetDefaultFolder(olFolderContacts).Parent.Folders("azerty")
    Set objContact = Folder.Items.Add(olContactItem)
    With objContact
      .Email1Address = "example@ex.com "
      .FirstName = "Joe"
      .LastName = "Mc"
      .HomeTelephoneNumber = "99 99 99 99 99"
      .HomeAddressCity = "Xlcity"
      .Save
    End With
    

    Or move the item to the target folder using the Move method after creation.

    Dim myolApp As Outlook.Application
    Dim myNamespace As Outlook.Namespace
    Set myolApp = CreateObject("Outlook.Application")
    Set myNamespace = myolApp.GetNamespace("MAPI")
    Set Folder = myNamespace.GetDefaultFolder(olFolderContacts).Parent.Folders("azerty")
    Set objContact = myolApp.CreateItem(olContactItem)
    With objContact
      .Email1Address = "example@ex.com "
      .FirstName = "Joe"
      .LastName = "Mc"
      .HomeTelephoneNumber = "99 99 99 99 99"
      .HomeAddressCity = "Xlcity"
      .Save
      .Move Folder 
    End With
    

    You can read more about that in the How To: Create a new Outlook Contact item programmatically article.

    0 讨论(0)
提交回复
热议问题