472,371 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 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 3946
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.