问题
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