Create a contact in a non-default Outlook contact folder

不羁岁月 提交于 2020-01-25 18:44:29

问题


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
\mypersonnal_pst\Contacts

I tried:

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
End With

回答1:


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.



来源:https://stackoverflow.com/questions/29326940/create-a-contact-in-a-non-default-outlook-contact-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!