By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,117 Members | 2,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,117 IT Pros & Developers. It's quick & easy.

Passing a FileSystemObject to a sub

P: n/a
*** post for FREE via your newsreader at post.newsfeed.com ***

Hello,

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

Consider the following code:

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

Private Sub Command1_Click()

Dim FileSys
Set FileSys = CreateObject("Scripting.FileSystemObject") 'Create a
FileSystemObject

Set ActiveFolder = FileSys.GetFolder("c:\temp\Ed_test") 'Defines active
folder

PerformDisplay (FileSys.GetFolder("c:\temp\Ed_test"))

End Sub

Private Sub PerformDisplay(ChosenObjectFolder)

Set FileList = ChosenObjectFolder.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_test"
folder. It works fine. (Note that I'm not using the ActiveFolder object.)

If I replace the line:
PerformDisplay (FileSys.GetFolder("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: ChosenObjectFolder is merely a string (its value is:
"c:\temp\Ed_test" ) .

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

Thank you very much!

John H. Dewbert

jo***********@yahoo.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! =-----

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
MRe
> Set FileSys = CreateObject("Scripting.FileSystemObject") 'Create a
FileSystemObject
Set ActiveFolder = FileSys.GetFolder("c:\temp\Ed_test") 'Defines active
folder

PerformDisplay (FileSys.GetFolder("c:\temp\Ed_test")) If I replace the line:
PerformDisplay (FileSys.GetFolder("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: ChosenObjectFolder is merely a string (its value is:
"c:\temp\Ed_test" ) .


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
Jul 17 '05 #2

P: n/a
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 "Intellisense" work:

Private FileSys as FileSystemObject

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

Private Sub Form_Unload
Set FileSys = Nothing
end sub

Private Sub Command1_Click()
Dim ActiveFolder as Folder '-- or as Scripting.Folder
Set ActiveFolder = FileSys.GetFolder("c:\temp\Ed_test")
PerformDisplay ActiveFolder
Set ActiveFolder = Nothing
end sub

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

--
--
John Dewbert <jo***********@yahoo.ca> wrote in message
news:40********@post.newsfeed.com...
*** post for FREE via your newsreader at post.newsfeed.com ***

Hello,

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

Consider the following code:

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

Private Sub Command1_Click()

Dim FileSys
Set FileSys = CreateObject("Scripting.FileSystemObject") 'Create a
FileSystemObject

Set ActiveFolder = FileSys.GetFolder("c:\temp\Ed_test") 'Defines active
folder

PerformDisplay (FileSys.GetFolder("c:\temp\Ed_test"))

End Sub

Private Sub PerformDisplay(ChosenObjectFolder)

Set FileList = ChosenObjectFolder.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_test"
folder. It works fine. (Note that I'm not using the ActiveFolder object.)
If I replace the line:
PerformDisplay (FileSys.GetFolder("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: ChosenObjectFolder is merely a string (its value is:
"c:\temp\Ed_test" ) .

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

Thank you very much!

John H. Dewbert

jo***********@yahoo.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! =-----

Jul 17 '05 #3

P: n/a
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

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!

Jul 17 '05 #4

P: n/a
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" <ma************@mindZZspring.com> wrote in message news:<Sx*****************@newsread3.news.atl.earth link.net>...
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 "Intellisense" work:

Private FileSys as FileSystemObject

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

Private Sub Form_Unload
Set FileSys = Nothing
end sub

Private Sub Command1_Click()
Dim ActiveFolder as Folder '-- or as Scripting.Folder
Set ActiveFolder = FileSys.GetFolder("c:\temp\Ed_test")
PerformDisplay ActiveFolder
Set ActiveFolder = Nothing
end sub

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

--
--
John Dewbert <jo***********@yahoo.ca> wrote in message
news:40********@post.newsfeed.com...
*** post for FREE via your newsreader at post.newsfeed.com ***

Hello,

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

Consider the following code:

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

Private Sub Command1_Click()

Dim FileSys
Set FileSys = CreateObject("Scripting.FileSystemObject") 'Create a
FileSystemObject

Set ActiveFolder = FileSys.GetFolder("c:\temp\Ed_test") 'Defines active
folder

PerformDisplay (FileSys.GetFolder("c:\temp\Ed_test"))

End Sub

Private Sub PerformDisplay(ChosenObjectFolder)

Set FileList = ChosenObjectFolder.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_test"
folder. It works fine. (Note that I'm not using the ActiveFolder

object.)

If I replace the line:
PerformDisplay (FileSys.GetFolder("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: ChosenObjectFolder is merely a string (its value is:
"c:\temp\Ed_test" ) .

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

Thank you very much!

John H. Dewbert

jo***********@yahoo.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! =-----

Jul 17 '05 #5

P: n/a
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" <jo************@hotmail.com> wrote in message
news:b1**************************@posting.google.c om...
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

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!

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.