Solved

automation import contacts

Posted on 2004-04-19
8
449 Views
Last Modified: 2008-02-26
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("Email")

   '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(olFolderContacts)
   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(olContactItem)
         ' 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
0
Comment
Question by:nomisp
  • 5
  • 3
8 Comments
 
LVL 12

Expert Comment

by:rcmb
Comment Utility
Try this:

Set olns = ol.GetNamespace("MAPI")
Set MyFolder1 = olns.GetDefaultFolder("olFolderContacts")
Set MyFolder2 = MyFolder1.Folders("Staff")

Look at KB articles 290804 (OL2K2), 208520 (OL2K) or 180696 (OL97)

RCMB
0
 
LVL 12

Expert Comment

by:rcmb
Comment Utility
Ignore that comment. I see you have done what I said.

RCMB
0
 
LVL 12

Expert Comment

by:rcmb
Comment Utility
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
0
 
LVL 2

Author Comment

by:nomisp
Comment Utility
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!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Accepted Solution

by:
rcmb earned 250 total points
Comment Utility
I agree fully that you are doing it right. My concern and I think your problem may be in the statement from the KB article you referenced:

Under Programming Considerations:
"The contacts are automatically placed in Outlook's default Contact folder, which is at the same level as the Inbox that receives incoming mail."

I tested what you built and exported my contacts to an mdb and then ran your program and I get the same problem that you have. It always goes to my contacts folder. I even tried going down two levels and it still goes to the contact folder. I believe you may have stumbled onto a limitation of the Outlook software.

I tried it on OL2002 and OL2003.

RCMB
0
 
LVL 2

Author Comment

by:nomisp
Comment Utility
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.

         
 
0
 
LVL 12

Expert Comment

by:rcmb
Comment Utility
Outstanding move ;-)

Thanks for the points and the good data!

RCMB
0
 
LVL 2

Author Comment

by:nomisp
Comment Utility
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!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Use email signature images to promote corporate certifications and industry awards.
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now