Results 1 to 3 of 3

Thread: Identify a single item in a files collection without a For Each ... Next
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Sep 2008
    Location
    God's Own County (Haworth, West Yorkshire, UK)
    Posts
    98
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Identify a single item in a files collection without a For Each ... Next

    Hi Guys.....

    I strongly suspect that the answer to this will be blindingly obvious, but here goes....
    The following code is the preamble to code which offers a filepicker if there is more than one file in a folder.
    Having identified that there is just one file in the folder, the following code gets its name, but looks and feels idiotic.
    Instead of the "For Each ... Next", what should I be doing?

    (Note: the "gstr..." variables are Public string variables set in other modules. The Watch facility verifies that they are correctly populated.)
    Code:
    Dim strFolder           As String
    Dim FSO                 As Object
    Dim objFilesCollection  As Object
    Dim objFile             As Object
      strFolder = gstrLatestsFolder & _
                  "\" & gstrProvider & _
                  "\" & gstrCompany & "\"
      Set FSO = CreateObject("Scripting.FileSystemObject")
      On Error Resume Next
      Set objFilesCollection = FSO.GetFolder(strFolder).Files
      If Err.Number = 0 Then
        If objFilesCollection.Count = 1 Then
          For Each objFile In objFilesCollection
            gstrLatestsFull = objFile.Name
          Next objFile
        End If
      End If
      On Error GoTo 0
    I've tried various combinations of
    Code:
    Set objFile = objFilesCollection(1)
    Set objFile = objFilesCollection.Files(1)
    but nothing works.
    Can any kind soul help?...
    Steve

    There's nothing so rare as common sense.
    The only stupid question is the one you DON'T ask!

  2. #2
    Board Regular
    Join Date
    Feb 2009
    Location
    Massachusetts, USA
    Posts
    1,977
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Identify a single item in a files collection without a For Each ... Next

    Hi Steve,

    Just a guess ...

    Maybe the array is zero based and it should be:

    Code:
    Set objFile = objFilesCollection(0)
    Gary
    Murphy's Fifth Law: If anything absolutely can NOT go wrong, it will anyway.

  3. #3
    Board Regular
    Join Date
    Sep 2008
    Location
    God's Own County (Haworth, West Yorkshire, UK)
    Posts
    98
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Identify a single item in a files collection without a For Each ... Next

    Gary....
    Possible, but the error I get is something like "Object doesn't support that method"
    Steve

    There's nothing so rare as common sense.
    The only stupid question is the one you DON'T ask!

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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