[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

automation import contacts

Posted on 2004-04-19
8
Medium Priority
?
459 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Large Outlook files lead to various unwanted errors and corruption issues. Furthermore, large outlook files can also make Outlook take longer to start-up, search, navigate, and shut-down. So, In this article, i will discuss a method to make your Out…
With so many activities to perform, Exchange administrators are always busy in organizations. If everything, including Exchange Servers, Outlook clients, and Office 365 accounts work without any issues, they can sit and relax. But unfortunately, it…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

650 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