how to pass a collection of contacts from a function

S

Southern at Heart

Dim ol As Object
Dim olns As Object
Dim objFolder As Object
Dim objAllContacts As Object
Dim Contact As Object
Dim counter As Integer
Set ol = New Outlook.Application
Set olns = ol.GetNamespace("MAPI")
Set objFolder = olns.PickFolder
If objFolder Is Nothing Then
Exit Sub
ElseIf objFolder.DefaultItemType <> olContactItem Then
Exit Sub
End If
Set objAllContacts = objFolder.Items

I have several subs that all start out with this code above to get a
collection of contacts
Can I make this a function that returns objAllContacts somehow? I've tried
something like this code below, but can't get it to work...

sub test()
Dim MyContacts as Object
MyContacts = GetContacts
....
End Sub


....I then made a function like this:

Function GetContacts()
Dim ol As Object
Dim olns As Object
Dim objFolder As Object
Dim objAllContacts As Object
Dim Contact As Object
Dim counter As Integer
Set ol = New Outlook.Application
Set olns = ol.GetNamespace("MAPI")
Set objFolder = olns.PickFolder
If objFolder Is Nothing Then
Exit Sub
ElseIf objFolder.DefaultItemType <> olContactItem Then
Exit Sub
End If
Set objAllContacts = objFolder.Items
GetContacts=objAllContacts
End Function

....but it doesn't work?
 
M

Michael Bauer [MVP - Outlook]

#1 This function returns a collection:

Funtion Sample1() as Outlook.Items
...
Set Sample1 = objFolder.Items
End

It's called like this:

Dim Result as Outlook.Items
Set Result=Sample1


#2 This uses the argument passed to the sub to return the result:

Sub Sample2(Collection as Outlook.Items)
..
Set Collection = objFolder.Items
End Sub

It's called like this:

Dim Result as Outlook.Items
Sample2 Result


#3 This uses a variable on the module level to make the data available to
others:

Private m_Result as Outlook.Items

Sub Sample3()
...
Set m_Result = objFolder.Items
End Sub

--
Best regards
Michael Bauer - MVP Outlook
Use Outlook Categories? This is Your Tool:
<http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>


Am Sun, 10 Feb 2008 20:08:02 -0800 schrieb Southern at Heart:
 

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