Solved

automation import contacts

Posted on 2004-04-19
8
454 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
ID: 10862310
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
ID: 10862332
Ignore that comment. I see you have done what I said.

RCMB
0
 
LVL 12

Expert Comment

by:rcmb
ID: 10862564
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 2

Author Comment

by:nomisp
ID: 10866915
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
 
LVL 12

Accepted Solution

by:
rcmb earned 250 total points
ID: 10867366
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
ID: 10867841
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
ID: 10867854
Outstanding move ;-)

Thanks for the points and the good data!

RCMB
0
 
LVL 2

Author Comment

by:nomisp
ID: 10878163
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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