exporting contacts as vcards with Visual Basic.

G

Guest

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?
 
M

Michael Bauer

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top