Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

automation import contacts

Posted on 2004-04-19
8
Medium Priority
?
460 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

In this article I discuss my selections of the Top Four free Outlook OST File Viewers available. Open, view and read even damaged OST files by using these tools. They all provide a clear preview of all data such as emails, notes, tasks, calendars, e…
Mailbox Corruption is a nightmare every Exchange DBA wishes he never has. Recovering from it can be super-hectic if not entirely futile. And though techniques like the New-MailboxRepairRequest cmdlet have been designed to help with fixing minor corr…
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…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

810 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