Results 1 to 14 of 14
  1. #1
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Array of Objects? (Outlook 2000 +)

    I'm learning something new, and need some assurance or direction. Apparently I can't create a Function which passes an Array of Objects ... true? But I am able to create a Collection of Objects and pass the Collection.

    My question has it's genesis in threads like <!post=this,285598>this<!/post>, from which I wanted to have a generic function to pass Outlook Items to any routine. I have ended up with this (errorhandling falls on the calling routine):

    Public Function GetSelectedItems() As Collection
    Dim intC As Integer
    Set GetSelectedItems = New Collection
    If TypeName(Application.ActiveWindow) = "Explorer" Then
    For intC = 1 To ActiveExplorer.Selection.Count
    GetSelectedItems.Add Item:=ActiveExplorer.Selection(intC)
    Next intC
    Else
    GetSelectedItems.Add Item:=ActiveInspector.CurrentItem
    End If
    End Function

    which works, but am I on the right track?
    -John ... I float in liquid gardens
    UTC -7ąDS

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Array of Objects? (Outlook 2000 +)

    Hi John,

    You can define a custom type that contains an array of objects, and use that:

    Public Type MyType
    MyArr() As Object
    End Type

    Public Function GetSelectedItems() As MyType
    ReDim arr(1 To ActiveExplorer.Selection.Count) As Object
    Dim i As Long

    For i = 1 To ActiveExplorer.Selection.Count
    Set arr(i) = ActiveExplorer.Selection(i)
    Next i

    GetSelectedItems.MyArr = arr
    End Function

    Sub Test()
    Debug.Print GetSelectedItems.MyArr(1).Subject
    End Sub

  3. #3
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Array of Objects? (Outlook 2000 +)

    John,
    You can return an array of objects from a function - for example:
    <pre>Public Function GetSelectedItems() As Object()
    Dim lngCount As Long, arrItems() As Object, n As Long
    If TypeName(Application.ActiveWindow) = "Explorer" Then
    With ActiveExplorer.Selection
    lngCount = .Count
    ReDim arrItems(1 To lngCount)
    For n = 1 To lngCount
    Set arrItems(n) = .Item(n)
    Next n
    End With
    Else
    ReDim arrItems(1)
    arrItems(1) = ActiveInspector.CurrentItem
    End If
    GetSelectedItems = arrItems
    End Function
    </pre>

    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Brussels, Brussel, Belgium
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Array of Objects? (Outlook 2000 +)

    hi John,

    that seem perfectly fine to me. the benefit that the collection offers is access of an item either through its key or index, the count property, the for each support (however this is also true for arrays), if you start to embed collections into class modules, you could build your own object model from that. the disadvantage being that error handling can sometimes become awkward.

  5. #5
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Array of Objects? (Outlook 2000 +)

    <img src=/S/doh.gif border=0 alt=doh width=15 height=15> I hate it when I overlook the obvious, which is to specify the function as an Object Array as you did. Thanks, Rory.

    Thanks also to Hans and Pieter.

    Any binding or memory advantages of using an Object Array vs. a Collection?
    -John ... I float in liquid gardens
    UTC -7ąDS

  6. #6
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Array of Objects? (Outlook 2000 +)

    Not that I'm aware of though you do get the advantages that Pieter mentioned. Using a collection is also easier to instantiate as you can simply set it equal to ActiveExplorer.Selection rather than looping through each item.
    FWIW
    Regards,
    Rory

    Microsoft MVP - Excel

  7. #7
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Array of Objects? (Outlook 2000 +)

    Cool, that really simplifies it Thanks again.
    -John ... I float in liquid gardens
    UTC -7ąDS

  8. #8
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Array of Objects? (Outlook 2000 +)

    Rory, guess I didn't understand. I tried this with the Collection Function version but the calling routine returns a type mismatch error processing the Outlook Message Items:

    If TypeName(Application.ActiveWindow) = "Explorer" Then
    GetSelectedItems.Add ActiveExplorer.Selection

    where looping through the selected items does work.
    -John ... I float in liquid gardens
    UTC -7ąDS

  9. #9
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Array of Objects? (Outlook 2000 +)

    John,
    Try:
    <pre>Set GetSelectedItems = activeexplorer.selection
    </pre>

    which I think is what I used ealier when testing.
    HTH.
    Regards,
    Rory

    Microsoft MVP - Excel

  10. #10
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Array of Objects? (Outlook 2000 +)

    In the 'Function GetSelectedItems() as Collection'

    Set GetSelectedItems = ActiveExplorer.Selection

    errors with a "type mismatch".

    I can use that same assignment in the 'Function GetSelectedItems() as Object()' array version you posted, in place of the loop, but then the calling sub errors out with "cannot assign to array" on the second line here:

    Dim colSelItems() As Object
    Set colSelItems = GetSelectedItems

    I should quit while I'm ahead, but curiosity prevents me. Thanks for your patience.
    -John ... I float in liquid gardens
    UTC -7ąDS

  11. #11
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Array of Objects? (Outlook 2000 +)

    Ah, I think the reason for both is the same. When I was testing I didn't declare the function as returning a collection - it returned a Variant. The same theory should work with the second one - dim ColSelItems as a Variant and then assign the array to it.
    HTH.
    Regards,
    Rory

    Microsoft MVP - Excel

  12. #12
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Brussels, Brussel, Belgium
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Array of Objects? (Outlook 2000 +)

    you cannot set a collection of your own to a collection that's built in the object model. you can only copy collections directly if both are created by the user, otherwise you need to loop.

    this will work:
    dim a as collection, b as collection
    set a=new collection
    a.add "1", "one"
    a.add "2", "two"
    set b=a

    but this won't:
    dim a as collection
    set a=new collection
    set a=activedocument.paragraphs'*type mismatch error*

    in this case you will need to loop to copy the paragraphs into a.
    the error is generated because vba doesn't know how to copy all properties and subobjects into the collection, and even if you create a matching class module, a loop will be necessary.

  13. #13
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Array of Objects? (Outlook 2000 +)

    OK, but then won't the calling routine need to type the Variant to handle it? If so I'd rather keep the loop and guarantee the data type passed to the caller, as Pieter suggests. All I have to do then is after an On Error Resume Next, set each Collection element as the intended data type and test that it's Not Nothing.

    Thanks for your help, Rory.
    -John ... I float in liquid gardens
    UTC -7ąDS

  14. #14
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Array of Objects? (Outlook 2000 +)

    John,
    You may also want to look at returning an Outlook.Selection object from the function rather than a Collection.
    FWIW.
    Regards,
    Rory

    Microsoft MVP - Excel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •