Solved

automation import contacts

Posted on 2004-04-19
8
456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

How to resolve IMCEAEX NDRs in Exchange or Exchange Online related to invalid X500 addresses.
If you troubleshoot Outlook for clients, you may want to know a bit more about the OST file before doing your next job. IMAP can cause a lot of drama if removed in the accounts without backing up.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

705 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