A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

how to pass a collection of contacts from a function



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old February 11th 08, 05:08 AM posted to microsoft.public.outlook.program_vba
Southern at Heart
external usenet poster
 
Posts: 34
Default how to pass a collection of contacts from a function

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?
Ads
  #2  
Old February 11th 08, 07:53 AM posted to microsoft.public.outlook.program_vba
Michael Bauer [MVP - Outlook]
external usenet poster
 
Posts: 1,885
Default how to pass a collection of contacts from a function



#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:

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?

 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Import function limiting contacts Nikki Outlook - Using Contacts 1 October 1st 07 07:05 AM
Problem in view Outlook 2003 contacts. Error " The properties dialog box could not be displayed. An invalid ENTRYID was pass" Jaweed Patel Outlook - Using Contacts 0 April 17th 07 11:31 AM
Search function in contacts doesn't work Cowtoon Outlook - Using Contacts 1 February 17th 07 09:35 PM
outlook contacts collection Dick Outlook and VBA 1 January 3rd 07 02:40 PM
Any function to print a Contacts page? fly2moon2 Outlook - Using Contacts 1 September 24th 06 04:09 PM


All times are GMT +1. The time now is 01:14 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.