在非默认Outlook联系人文件夹中创建联系人

时间:2015-03-29 08:03:52

标签: excel-vba outlook-2010 vba excel

  

您好,我想在Excel VBA 2010的非默认Outlook联系人文件夹中创建联系人。   在此示例中,文件夹名称为“azerty”,位于\ mypersonnal_pst \ Contacts中   我尝试失败了:

  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
  

我试过你的例子'addnewcontacts'。这是行不通的。   我将不胜感激任何帮助。 THKS

1 个答案:

答案 0 :(得分:0)

如果需要在特定文件夹中创建Outlook项,请改用Items类的Add方法。

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.Add(olContactItem)
With objContact
  .Email1Address = "example@ex.com "
  .FirstName = "Joe"
  .LastName = "Mc"
  .HomeTelephoneNumber = "99 99 99 99 99"
  .HomeAddressCity = "Xlcity"
  .Save
End With

或者在创建后使用Move方法将项​​目移动到目标文件夹。

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

您可以在How To: Create a new Outlook Contact item programmatically文章中详细了解相关内容。