Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Outlook / Programming VBA / October 2005

Tip: Looking for answers? Try searching our database.

exporting contacts as vcards with Visual Basic.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jonathan Brown - 30 Oct 2005 00:17 GMT
I couldn't find the Visual Basic 6 forum so I thought I'd put this in here.  
It applies to Outlook as well.  I'm using Visual Basic 6 to develop a form
that will export all my contacts as vcard files.  It's supposed to save the
files to my c:\vcards folder as you should see.  But visual basic is choking
on my for each...next loop.  Here is what I have as my procedure:

Private Sub cmdExport_Click()
   Dim oContactsFolder As Outlook.MAPIFolder
   Dim oNameSpace As NameSpace
   Dim oApp As Outlook.Application
   Dim oContact As Outlook.ContactItem
   Dim syncSuccess As Boolean
           
   Set oApp = CreateObject("Outlook.Application")
   Set oNameSpace = oApp.GetNamespace("MAPI")
   Set oContactsFolder = oNameSpace.GetDefaultFolder(olFolderContacts)
                   
   'On Error GoTo CreateContacts_Error
   
       
   'Create New vcard Items
   For Each oContact In oContactsFolder
       oContact.SaveAs "c:\vcards\" & oContact.FileAs & ".vcf", olVCard
   Next oContact
       
   syncSuccess = True
             
CreateContacts_Exit:
   If syncSuccess = True Then
       MsgBox "Synchronization was successful", vbInformation
   End If
   
   Exit Sub

CreateContacts_Error:
   MsgBox "Error#: " & Err.Number & vbCr & Err.Description, vbInformation
   syncSuccess = False
   GoTo CreateContacts_Exit
End Sub

when I try to run this procedure I get an error message stating the "Object
doesn't support this property or method".  When I click debug the highlighted
line is the "for each ocontact in ocontactsfolder" line.

Any reason why I shouldn't be able to loop through my contacts folder and
save each contact as a vcard file?  
Michael Bauer - 30 Oct 2005 10:03 GMT
Am Sat, 29 Oct 2005 16:17:02 -0700 schrieb Jonathan Brown:

Jonathan, in fact you can´t loop through the folder object, but you need to
loop through its Items collection.

The next error you could be faced with is that a contact folder also can
contain distributionlists. In that case you´d get an error 13 in the same
line.

So a better approach is this one:
Dim obj as object

For Each obj in oContactsFolder.Items
    If Typeof obj is Outlook.ContactItem Then
        Set oContact = obj
        ...
    Endif
Next

Signature

Viele Gruesse / Best regards
Michael Bauer - MVP Outlook

> I couldn't find the Visual Basic 6 forum so I thought I'd put this in here.  
> It applies to Outlook as well.  I'm using Visual Basic 6 to develop a form
[quoted text clipped - 42 lines]
> Any reason why I shouldn't be able to loop through my contacts folder and
> save each contact as a vcard file?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.