Treeview - looping through nodes


I am simply trying to loop through the nodes of my treeview control with following code:
With TaskItemsForm.trvTasks
        For Each n In trvTasks
            Debug.Print "Key :  ", .Nodes.Item.Key, "Text :   ", .Nodes.Item.Text
        Next n
     End With

Open in new window

but I am getting the error :
Compile error - argument not optional - and it highlights the ".Items" section of .Nodes.Item.Key

Any help on guiding me through this would be appreciated.

Best regards,

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:

Why aren't you using the loop variable n?

Have you checked what it is?

Is it a node?

Try this.
Dim N As Node

        For Each N In trvTasks.Nodes
            Debug.Print "Key :  ", N.Key, "Text :   ", N.Text
        Next N

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
Here's code to "Walk" the nodes of a treeview.

Private Sub WalkTree(ByVal ParentNode As Node)
    Do Until ParentNode Is Nothing
        Debug.Print ParentNode.Text, ParentNode.Index
        WalkTree ParentNode.Child
        Set ParentNode = ParentNode.Next

End Sub

Open in new window


WalkTree trvTasks.Nodes(1)
slobber72Author Commented:
Thanks guys. I am still a relative novice in coding - the different uses of methods / properties etc still gets me befuddled. You have both given me a way to cycle through the treeview and they both work really well with my code.
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

slobber72Author Commented:
Hi Martin,

I am getting an error when trying to call your procedure- as I am hopeless with VBA in Excel I can't track down what the reason for it is.

When I call the procedure as below, I get the error "Type Mismatch"...

Private Sub UserForm_Initialize()
    With Me
        'Label the userform
        .Caption = "Menu Tasks Comment Form"
        ' Label the buttons
        .CommandButton1.Caption = "Close"
        .CommandButton2.Caption = "Add Comment"
        .CommandButton3.Caption = "View Comments"
        .Frame1.Caption = "Comments"
        'Null out the label
        .Label1 = vbNullString
        .TextBox1 = vbNullString
        'Label the treeview
        .TreeView1.LineStyle = tvwRootLines
        'Sort the treeview
        .TreeView1.Sorted = True
    End With
   'Populate the Treeview
    TreeView_Populate True ' populate the treeview control
    WalkTree Me.TreeView1.Nodes(1)
End Sub

Open in new window

Could you perhaps point me in the right direction as to what I am doing wrong ?

Thanks in advance.
slobber72Author Commented:
Hi again,

I found that I needed to change the procedure to declare the parentnode as an object, not as a node - it would appear that this is a version of treeview issue.

Private Sub WalkTree(ByVal ParentNode As Object) ' previously declared as node
    Do Until ParentNode Is Nothing
        Debug.Print ParentNode.Text, , ParentNode.Index, , ParentNode.Key
        WalkTree ParentNode.Child
        Set ParentNode = ParentNode.Next

End Sub

Open in new window

Thanks again !!
Martin LissOlder than dirtCommented:
I'm glad you were able to fix it. Not that I'm complaining but what happened between the time you said my code worked really well and the type mismatch error?

Just out of curiosity, does the following work for you?

Private Sub WalkTree(ByVal ParentNode As ComctlLib.Node)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.