Solved

Accessing collection element.

Posted on 2006-12-01
12
215 Views
Last Modified: 2010-04-30
Hi there,

***********************
        Dim fso As Object
        Dim fld As Object
        Dim subfs As Object
        Dim subf As Object
       
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set fld = fso.GetFolder(path)
        Set subfs = fld.SubFolders
        Set subf = subfs.Item(2)
***********************

I get an error on the last line, "Invalid procedure call or argument"...
0
Comment
Question by:Unimatrix_001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
  • +1
12 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 165 total points
ID: 18059049
You have to access it via the key name, not the index, so
set  subf = subf.Item("foldername")
It seems a little pointless, as most of the time you are creating the collection to get the folder names.

You can still iterate through the collection with a for ... each statement

  For Each subf In subfs
     MsgBox subf.Name
  Next
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18059057
What I'm after is getting hold of the last folder in that collection, is there a tidy way to do it without a for loop?
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 170 total points
ID: 18059069
>What I'm after is getting hold of the last folder in that collection, is there a tidy way to do it without a for loop?
the "last" means the list needs to be ordered. however, the collections are not ordered in a guaranteed way, so you need
1) to specify the "order"
2) order by that property
3) get the last from there.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 29

Expert Comment

by:Nightman
ID: 18059071
Without knowing the key name, not that I know of.

I agree with you, it seems kind of dumb that you can't get it by index and having to know the key name to reference the object (why else would you be iterationg through in the first place, if not to get the name!).
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18059073
angelIII: Could you expand on that please, are those methods I can use etc... ?
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18059074
"Without knowing the key name, not that I know of."
Hmm, okay. Although yes, seems incredibly silly not getting an element by index!
0
 
LVL 11

Assisted Solution

by:Dany Balian
Dany Balian earned 165 total points
ID: 18059242
try this

        Dim fso As Object
        Dim fld As Object
        Dim subfs As Object
        Dim subf As Object, subfol As Object
       
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set fld = fso.GetFolder("C:\")
        Set subfs = fld.SubFolders
        For Each subf In subfs
            Set subfol = subf
        Next
       
        MsgBox subfol.Name  '<< this will give u the last item in the list...


hope this helps..

dan

0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18059253
mrdany: Thanks, although what I'm after is:

"is there a tidy way to do it without a for loop?"

Although, I'll wait for AngelIII to reply, and then I'll probably split the points. :)
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18059281
>"is there a tidy way to do it without a for loop?"
no.
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 18059790
sorry unimatrix, i didnt read the comments.. i just read the main question and answered...
my bad!!
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18060346
Hrm, okay... Thanks any everybody for your contributions. Points split. :)
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18060348
mrdany: Np mate. ;)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question