nomisp
asked on
automation import contacts
Hi,
I'm trying to import contacts to a contacts subfolder using a VBA macro. Trouble is they keep importing into the main contacts folder rather than the sub folder. Below is what i have(modified from MS KB 170320).
Any help much appreciated...
Sub ImportContacts()
'Set up DAO Objects:
Dim oDataBase As Object
Dim rst As Object
Set oDataBase = OpenDatabase _
("C:\Email.mdb")
Set rst = oDataBase.OpenRecordset("E mail")
'Set up Outlook Objects:
Dim olns As Object ' Outlook Namespace
Dim Contacts As Object ' Main Contact Folder
Dim cf As Object ' Contact folder
Dim c As Object ' Contact Item
Dim Prop As Object ' User property
Dim ol As New Outlook.Application
Set olns = ol.GetNamespace("MAPI")
Set Contacts = olns.GetDefaultFolder(olFo lderContac ts)
Set cf = Contacts.Folders("Staff")
With rst
.MoveFirst
' Loop through the Access records
Do While Not .EOF
' Create a new Contact item
Set c = ol.CreateItem(olContactIte m)
' Specify which Outlook form to use:
' Change "IPM.Contact" to "IPM.Contact.<formname>" if you've
' created a custom Contact form in Outlook.
c.MessageClass = "IPM.Contact"
' Create all built-in Outlook fields
If ![Staffname] <> "" Then c.FullName = ![Staffname]
If ![Staff_Email] <> "" Then c.Email1Address = ![Staff_Email]
' Save the contact
c.Save
.MoveNext
Loop
End With
End Sub
I'm trying to import contacts to a contacts subfolder using a VBA macro. Trouble is they keep importing into the main contacts folder rather than the sub folder. Below is what i have(modified from MS KB 170320).
Any help much appreciated...
Sub ImportContacts()
'Set up DAO Objects:
Dim oDataBase As Object
Dim rst As Object
Set oDataBase = OpenDatabase _
("C:\Email.mdb")
Set rst = oDataBase.OpenRecordset("E
'Set up Outlook Objects:
Dim olns As Object ' Outlook Namespace
Dim Contacts As Object ' Main Contact Folder
Dim cf As Object ' Contact folder
Dim c As Object ' Contact Item
Dim Prop As Object ' User property
Dim ol As New Outlook.Application
Set olns = ol.GetNamespace("MAPI")
Set Contacts = olns.GetDefaultFolder(olFo
Set cf = Contacts.Folders("Staff")
With rst
.MoveFirst
' Loop through the Access records
Do While Not .EOF
' Create a new Contact item
Set c = ol.CreateItem(olContactIte
' Specify which Outlook form to use:
' Change "IPM.Contact" to "IPM.Contact.<formname>" if you've
' created a custom Contact form in Outlook.
c.MessageClass = "IPM.Contact"
' Create all built-in Outlook fields
If ![Staffname] <> "" Then c.FullName = ![Staffname]
If ![Staff_Email] <> "" Then c.Email1Address = ![Staff_Email]
' Save the contact
c.Save
.MoveNext
Loop
End With
End Sub
Ignore that comment. I see you have done what I said.
RCMB
RCMB
The only thing that I see as possibly wrong is the line
Dim ol As New Outlook.Application
Try using:
Set ol = New Outlook.Application
Also these are good articles: Look at KB articles 290804 (OL2K2), 208520 (OL2K) or 180696 (OL97)
RCMB
Dim ol As New Outlook.Application
Try using:
Set ol = New Outlook.Application
Also these are good articles: Look at KB articles 290804 (OL2K2), 208520 (OL2K) or 180696 (OL97)
RCMB
ASKER
Thanks rcmb but no luck. Changing to set ol = New Outlook.Application has no effect and the kb articles seem to show that i'm doing it right!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If you can't solve a problem..go around it! I added the line below after the c.save line to move each contact to the correct folder.
c.Move Contacts.Folders("Staff")
Thanks for your time.
c.Move Contacts.Folders("Staff")
Thanks for your time.
Outstanding move ;-)
Thanks for the points and the good data!
RCMB
Thanks for the points and the good data!
RCMB
ASKER
Just for completeness here is how to do it properly..
1) To get a non-default folder, you need to walk the folder hierarchy using the Folders collections or use a function that does that for you. See http://www.slipstick.com/dev/code/getfolder.htm . You then use the MAPIFolder.Items.ItemAdd method, instead of CreateItem, to create an item directly in that folder.
taken from - http://www.outlookcode.com/codedetail.aspx?id=244
So there you go!
1) To get a non-default folder, you need to walk the folder hierarchy using the Folders collections or use a function that does that for you. See http://www.slipstick.com/dev/code/getfolder.htm . You then use the MAPIFolder.Items.ItemAdd method, instead of CreateItem, to create an item directly in that folder.
taken from - http://www.outlookcode.com/codedetail.aspx?id=244
So there you go!
Set olns = ol.GetNamespace("MAPI")
Set MyFolder1 = olns.GetDefaultFolder("olF
Set MyFolder2 = MyFolder1.Folders("Staff")
Look at KB articles 290804 (OL2K2), 208520 (OL2K) or 180696 (OL97)
RCMB