PDA

View Full Version : Solved: Problem with Typename for Multipage/Page in recursive function



Cosmo
06-27-2008, 09:10 AM
I need to run a function (function 'add' in the code) on a MultiPage in a UserForm, and run the same function for every Page. Since both functions I have listed here need to be run on multiple object types (UserForm, MultiPage, Page), the parameters are passed as generic object type. I am checking the type of control (MultiPage or Page) by using TypeName(item) and find that when I call the function with a 'Page', it changes to type 'Controls' in the recursive function.


Here are the parts of the code to show the problem (NOTE - there is a lot more code involved, I have removed the function steps that I perform on the items to show just the recursion)
Private Function test()
Add UserForm1.MultiPage1
End Function
Public Function Add(ByRef Item As Object)
On Error GoTo errorcode
Dim oPage As Page
Dim objectType As String
Debug.Print "Item is type '" & TypeName(Item) & "'."
objectType = processItem(Item)
If TypeOf Item Is MultiPage Then
Dim i As Integer
Dim oMultipage As MultiPage
Set oMultipage = Item
For i = 1 To oMultipage.Pages.Count
Set oPage = oMultipage.Pages(i - 1)
Debug.Print "Page '" & oPage.Name & "' is type '" & TypeName(oPage) & "'."
Add (oPage)
Next i
End If
Debug.Print "Item is now type '" & TypeName(Item) & "'."
Debug.Print
Exit Function
errorcode:
Debug.Print "DialogCollection.Add - " & Err.Description
Resume Next
End Function
Private Function processItem(ByRef theObject As Object) As String
On Error GoTo errorcode
' NOTE-processing steps removed - this function recurses too.
Debug.Print "theObject is type '" & TypeName(theObject) & "'."
processItem = TypeName(theObject)
Exit Function
errorcode:
Debug.Print "Error processItem - " & Err.Description
processItem = ""
Resume Next
End Function

Cosmo
06-27-2008, 12:03 PM
I realized that the second function wasn't needed in the example, so I've boiled my code down to a bare minimum here.
Private Function test()
Add UserForm1.MultiPage1
End Function

Public Function Add(ByRef Item As Object)
Dim oPage As Page
Debug.Print "Item is type '" & TypeName(Item) & "'."
If TypeOf Item Is MultiPage Then
Dim i As Integer
For i = 1 To Item.Pages.Count
Set oPage = Item.Pages(i - 1)
Debug.Print "Page '" & oPage.Name & "' is type '" & TypeName(oPage) & "'."
Add (oPage)
Next i
End If
End Function


So, can anyone tell me why the Page is of type 'Page' before being passed to the 'Add' function (on line 13), but is type 'Controls' directly after being passed to the function?

If I add a line
Set oPage = Item

even that fails with the Page that has been passed into the function (so it's no longer a Page even though it was one before being passed into the functtion?)


Can anyone tell me what I'm doing wrong?

Cosmo
06-27-2008, 02:08 PM
D'oh, I hate when it's a simple mistake. Found the solution - It was the line:
Add (oPage)

changed it to
Add oPage

and everything works fine.