473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Passing a FileSystemObject to a sub

*** 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
5 4027
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Marcelo Rizzo | last post by:
I am trying to get the name of a file with a specific extension (tmw) from several different directories. The problem I am having is that the program stops working on the second pass with an run...
3
by: Steve | last post by:
Hi all, FileSystemObject question... I'm trying to check if files exist or not, but the files are not sitting on the same server as our Intranet. Basically we have about 5000 photos in a...
1
by: Rob Oliver | last post by:
Hi, I've seen a walkthrough for passing data from a setup project to a custom action (an application) with an InstallClass. I am curious though--can the data also be intercepted by a custom...
2
by: Sean S - Perth, WA | last post by:
Hi all, If I create a FileSystemObject Object is it appropriate to reuse it to perform operations on different files/folders? Or should I create a new FileSystemObject Object for each file and...
9
by: kermit | last post by:
I keep seeing that you can use the FileSystemObject in either VB script, or Javascript on an aspx page. I added a refrence to the scrrun.dll I added importing namespaces for 'System.Object',...
2
by: Luis Esteban Valencia | last post by:
I'm trying to write a little VB.NET script in an ASP.NET web page that will create folders, move files, etc... pretty much everything you can expect to do using the FileSystemObject library. Is...
1
by: G Gerard | last post by:
Hello I am using the FileSystemObject to copy files on a computer Dim MyObject as Object
3
by: nagmvs | last post by:
hello I have a problem with FileSystemObject in Vb Suppose i want to declare as Dim fs As New FileSystemObject, when i type New After that i can't see FileSystemObject in derived DropDownList. ...
12
lee123
by: lee123 | last post by:
why is this so hard to do. i have alot of books that have exaples of how to do these things but when i try the exaples they never work i have a previous post on "copying files" some of the other guys...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.