Passing a FileSystemObject to a sub

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Dewbert

    Passing a FileSystemObject to a sub

    *** post for FREE via your newsreader at post.newsfeed.c om ***

    Hello,

    I have trouble passing a folder object (from a FileSystemObjec t) to a sub
    procedure.

    Consider the following code:

    =============== =============== ===

    Private Sub Command1_Click( )

    Dim FileSys
    Set FileSys = CreateObject("S cripting.FileSy stemObject") 'Create a
    FileSystemObjec t

    Set ActiveFolder = FileSys.GetFold er("c:\temp\Ed_ test") 'Defines active
    folder

    PerformDisplay (FileSys.GetFol der("c:\temp\Ed _test"))

    End Sub



    Private Sub PerformDisplay( ChosenObjectFol der)

    Set FileList = ChosenObjectFol der.Files ' Create a collection
    For Each fil In FileList
    MsgBox (fil)
    Next fil

    End Sub

    =============== =============== ==



    What it does is prompting all the files existing in the "C:\temp\Ed_tes t"
    folder. It works fine. (Note that I'm not using the ActiveFolder object.)

    If I replace the line:
    PerformDisplay (FileSys.GetFol der("c:\temp\Ed _test"))
    by the line:
    PerformDisplay (ActiveFolder)

    then it stops working... In the Command1_Click sub, ActiveFolder is an
    object. However, what is passed to the PerformDisplay sub is not an object
    anymore: ChosenObjectFol der is merely a string (its value is:
    "c:\temp\Ed_tes t" ) .

    Question: how can I pass the object instead of the string?

    Thank you very much!

    John H. Dewbert

    john_hdewbert@y ahoo.ca






    -----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
    http://www.newsfeed.com - The #1 Newsgroup Service in the World!
    -----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----

  • MRe

    #2
    Re: Passing a FileSystemObjec t to a sub

    > Set FileSys = CreateObject("S cripting.FileSy stemObject") 'Create a[color=blue]
    > FileSystemObjec t
    > Set ActiveFolder = FileSys.GetFold er("c:\temp\Ed_ test") 'Defines active
    > folder
    >
    > PerformDisplay (FileSys.GetFol der("c:\temp\Ed _test"))[/color]
    [color=blue]
    > If I replace the line:
    > PerformDisplay (FileSys.GetFol der("c:\temp\Ed _test"))
    > by the line:
    > PerformDisplay (ActiveFolder)
    >
    > then it stops working... In the Command1_Click sub, ActiveFolder is an
    > object. However, what is passed to the PerformDisplay sub is not an[/color]
    object[color=blue]
    > anymore: ChosenObjectFol der is merely a string (its value is:
    > "c:\temp\Ed_tes t" ) .[/color]

    Try...

    PerformDisplay ActiveFolder

    ....[remove the brackets] -- Why? Not perfectly clear on why this is the
    case, but I believe it may have something to do with the fact that
    PerformDisplay is not a function (also the case if it was a function but not
    using the return value); this means that the brackets are being used, not to
    contain parameters, but for operation precedence, thus ActiveFolder is not
    being passed directly to the sub, it's being evaluated (to it's default
    value), then being passed -- then again, I'm just guessing!

    Regards,
    MRe


    Comment

    • mayayana

      #3
      Re: Passing a FileSystemObjec t to a sub

      You're writing script. There are no datatypes in your code,
      so everything is a variant.
      Also, if you set a reference to the Microsoft Scripting Runtime
      you can access the FSO as early-bound which is quicker and
      makes "Intellisen se" work:

      Private FileSys as FileSystemObjec t

      '--assuming that you're using it for the life of the form:
      Private Sub Form_Load()
      Set FileSys = New FileSystemObjec t
      End Sub

      Private Sub Form_Unload
      Set FileSys = Nothing
      end sub

      Private Sub Command1_Click( )
      Dim ActiveFolder as Folder '-- or as Scripting.Folde r
      Set ActiveFolder = FileSys.GetFold er("c:\temp\Ed_ test")
      PerformDisplay ActiveFolder
      Set ActiveFolder = Nothing
      end sub

      Private Sub PerformDisplay( ChosenObjectFol der as Folder)
      Dim FileList as Files '-- or Scripting.Files
      Dim fil as File '-- or Scripting.File
      Set FileList = ChosenObjectFol der.Files ' Create a collection
      For Each fil In FileList
      MsgBox (fil.Name)
      Next fil
      Set FileList = Nothing
      End Sub

      --
      --
      John Dewbert <john_hdewbert@ yahoo.ca> wrote in message
      news:40621599$1 @post.newsfeed. com...[color=blue]
      > *** post for FREE via your newsreader at post.newsfeed.c om ***
      >
      > Hello,
      >
      > I have trouble passing a folder object (from a FileSystemObjec t) to a sub
      > procedure.
      >
      > Consider the following code:
      >
      > =============== =============== ===
      >
      > Private Sub Command1_Click( )
      >
      > Dim FileSys
      > Set FileSys = CreateObject("S cripting.FileSy stemObject") 'Create a
      > FileSystemObjec t
      >
      > Set ActiveFolder = FileSys.GetFold er("c:\temp\Ed_ test") 'Defines active
      > folder
      >
      > PerformDisplay (FileSys.GetFol der("c:\temp\Ed _test"))
      >
      > End Sub
      >
      >
      >
      > Private Sub PerformDisplay( ChosenObjectFol der)
      >
      > Set FileList = ChosenObjectFol der.Files ' Create a collection
      > For Each fil In FileList
      > MsgBox (fil)
      > Next fil
      >
      > End Sub
      >
      > =============== =============== ==
      >
      >
      >
      > What it does is prompting all the files existing in the "C:\temp\Ed_tes t"
      > folder. It works fine. (Note that I'm not using the ActiveFolder[/color]
      object.)[color=blue]
      >
      > If I replace the line:
      > PerformDisplay (FileSys.GetFol der("c:\temp\Ed _test"))
      > by the line:
      > PerformDisplay (ActiveFolder)
      >
      > then it stops working... In the Command1_Click sub, ActiveFolder is an
      > object. However, what is passed to the PerformDisplay sub is not an[/color]
      object[color=blue]
      > anymore: ChosenObjectFol der is merely a string (its value is:
      > "c:\temp\Ed_tes t" ) .
      >
      > Question: how can I pass the object instead of the string?
      >
      > Thank you very much!
      >
      > John H. Dewbert
      >
      > john_hdewbert@y ahoo.ca
      >
      >
      >
      >
      >
      >
      > -----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
      > http://www.newsfeed.com - The #1 Newsgroup Service in the World!
      > -----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----
      >[/color]


      Comment

      • John Dewbert

        #4
        Re: Passing a FileSystemObjec t to a sub

        MRe, thank you very much!

        It works... and it makes sense! It also made me realise that instead of using:

        MsgBox (fil)

        I should formally use:

        MsgBox (fil.Name)

        John
        [color=blue]
        >
        > Try...
        >
        > PerformDisplay ActiveFolder
        >
        > ...[remove the brackets] -- Why? Not perfectly clear on why this is the
        > case, but I believe it may have something to do with the fact that
        > PerformDisplay is not a function (also the case if it was a function but not
        > using the return value); this means that the brackets are being used, not to
        > contain parameters, but for operation precedence, thus ActiveFolder is not
        > being passed directly to the sub, it's being evaluated (to it's default
        > value), then being passed -- then again, I'm just guessing![/color]

        Comment

        • John Dewbert

          #5
          Re: Passing a FileSystemObjec t to a sub

          Hello mayayana,

          Thanks for you tip about the reference to the Microsoft Scripting
          Runtime... I didn't know how to use dim for the objects created.
          Your comment was quite helpful. :-)

          John.

          "mayayana" <mayaXXyaYYna1a @mindZZspring.c om> wrote in message news:<Sxq8c.530 9$V66.2824@news read3.news.atl. earthlink.net>. ..[color=blue]
          > You're writing script. There are no datatypes in your code,
          > so everything is a variant.
          > Also, if you set a reference to the Microsoft Scripting Runtime
          > you can access the FSO as early-bound which is quicker and
          > makes "Intellisen se" work:
          >
          > Private FileSys as FileSystemObjec t
          >
          > '--assuming that you're using it for the life of the form:
          > Private Sub Form_Load()
          > Set FileSys = New FileSystemObjec t
          > End Sub
          >
          > Private Sub Form_Unload
          > Set FileSys = Nothing
          > end sub
          >
          > Private Sub Command1_Click( )
          > Dim ActiveFolder as Folder '-- or as Scripting.Folde r
          > Set ActiveFolder = FileSys.GetFold er("c:\temp\Ed_ test")
          > PerformDisplay ActiveFolder
          > Set ActiveFolder = Nothing
          > end sub
          >
          > Private Sub PerformDisplay( ChosenObjectFol der as Folder)
          > Dim FileList as Files '-- or Scripting.Files
          > Dim fil as File '-- or Scripting.File
          > Set FileList = ChosenObjectFol der.Files ' Create a collection
          > For Each fil In FileList
          > MsgBox (fil.Name)
          > Next fil
          > Set FileList = Nothing
          > End Sub
          >
          > --
          > --
          > John Dewbert <john_hdewbert@ yahoo.ca> wrote in message
          > news:40621599$1 @post.newsfeed. com...[color=green]
          > > *** post for FREE via your newsreader at post.newsfeed.c om ***
          > >
          > > Hello,
          > >
          > > I have trouble passing a folder object (from a FileSystemObjec t) to a sub
          > > procedure.
          > >
          > > Consider the following code:
          > >
          > > =============== =============== ===
          > >
          > > Private Sub Command1_Click( )
          > >
          > > Dim FileSys
          > > Set FileSys = CreateObject("S cripting.FileSy stemObject") 'Create a
          > > FileSystemObjec t
          > >
          > > Set ActiveFolder = FileSys.GetFold er("c:\temp\Ed_ test") 'Defines active
          > > folder
          > >
          > > PerformDisplay (FileSys.GetFol der("c:\temp\Ed _test"))
          > >
          > > End Sub
          > >
          > >
          > >
          > > Private Sub PerformDisplay( ChosenObjectFol der)
          > >
          > > Set FileList = ChosenObjectFol der.Files ' Create a collection
          > > For Each fil In FileList
          > > MsgBox (fil)
          > > Next fil
          > >
          > > End Sub
          > >
          > > =============== =============== ==
          > >
          > >
          > >
          > > What it does is prompting all the files existing in the "C:\temp\Ed_tes t"
          > > folder. It works fine. (Note that I'm not using the ActiveFolder[/color]
          > object.)[color=green]
          > >
          > > If I replace the line:
          > > PerformDisplay (FileSys.GetFol der("c:\temp\Ed _test"))
          > > by the line:
          > > PerformDisplay (ActiveFolder)
          > >
          > > then it stops working... In the Command1_Click sub, ActiveFolder is an
          > > object. However, what is passed to the PerformDisplay sub is not an[/color]
          > object[color=green]
          > > anymore: ChosenObjectFol der is merely a string (its value is:
          > > "c:\temp\Ed_tes t" ) .
          > >
          > > Question: how can I pass the object instead of the string?
          > >
          > > Thank you very much!
          > >
          > > John H. Dewbert
          > >
          > > john_hdewbert@y ahoo.ca
          > >
          > >
          > >
          > >
          > >
          > >
          > > -----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
          > > http://www.newsfeed.com - The #1 Newsgroup Service in the World!
          > > -----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----
          > >[/color][/color]

          Comment

          • Steve Gerrard

            #6
            Re: Passing a FileSystemObjec t to a sub

            MRe is quite right about why
            PerformDisplay (ActiveFolder)
            doesn't work, while
            PerformDisplay ActiveFolder
            does.

            Another option, the one I prefer, is to use
            Call PerformDisplay( ActiveFolder)

            Notice that there is no space between the y and the (. This shows that
            the variable in parens is a being passed as a parameter, not evaluated
            as an expression, just as it would be with a function.

            Your observation about using fil.Name instead of just fil is also a good
            one. Many VB'ers make a habit of writing, for instance, Text1.Text =
            "Hello", instead of just Text1 = "Hello", and basically never rely on
            default properties.

            "John Dewbert" <john_h_dewbert @hotmail.com> wrote in message
            news:b1cd77a7.0 403250703.24606 [email protected] gle.com...[color=blue]
            > MRe, thank you very much!
            >
            > It works... and it makes sense! It also made me realise that instead[/color]
            of using:[color=blue]
            >
            > MsgBox (fil)
            >
            > I should formally use:
            >
            > MsgBox (fil.Name)
            >
            > John
            >[color=green]
            > >
            > > Try...
            > >
            > > PerformDisplay ActiveFolder
            > >
            > > ...[remove the brackets] -- Why? Not perfectly clear on why this is[/color][/color]
            the[color=blue][color=green]
            > > case, but I believe it may have something to do with the fact that
            > > PerformDisplay is not a function (also the case if it was a function[/color][/color]
            but not[color=blue][color=green]
            > > using the return value); this means that the brackets are being[/color][/color]
            used, not to[color=blue][color=green]
            > > contain parameters, but for operation precedence, thus ActiveFolder[/color][/color]
            is not[color=blue][color=green]
            > > being passed directly to the sub, it's being evaluated (to it's[/color][/color]
            default[color=blue][color=green]
            > > value), then being passed -- then again, I'm just guessing![/color][/color]


            Comment

            Working...