Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's wrong with this code 4

Status
Not open for further replies.

wmbb

Technical User
Jul 17, 2005
320
NL
The code below is showing a compile error "user-defined type not defined" in the line Dim fso...
Can someone tell me what's wrong ?

Code:
Sub test()
Dim fso As FileSystemObject
Dim f As File
  Set fso = CreateObject("Scripting.FileSystemObject")
    For Each f In fso.GetFolder("C:\temp").Files
      MsgBox f.Name
    Next
  Set fso = Nothing
End Sub
 
Do you have a reference to Microsoft Scripting Runtime?
 
use instead...
Code:
Dim fso As Object


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Where can I find the information for the reference to scripting runtime.
I took this piece of code from another document with a working macro.

Changing to Dim fso As Object gives the same error but now for the line Dim f As File ?!
 
Code:
Dim f As Object

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
You can discover a few things about these objects using faq707-4594

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi Skip,

That works, Thanks.
Just for my understanding....
Why does the first code work in the original word doc but not in the document I created.
Looking on the internet they all use the original code ??!!
 
wmbb said:
Where can I find the information for the reference to scripting runtime.

When editing your code in VBA, select Tools / References and scroll down to find Microsoft Scripting Runtime.

[blue]Dim F As File[/blue] is having the same problem because the "File" object is also defined in Microsoft Scripting Runtime. The original Word document probably has the reference set and yours does not.

Incidently, you are mixing early and late binding. Early binding works like this
Code:
Sub test()
Dim fso As FileSystemObject
Dim f As File
  Set fso = New FileSystemObject
  [blue]etc.[/blue]

While late binding is
Code:
Sub test()
Dim fso As Object
Dim f As Variant
  Set fso = CreateObject("Scripting.FileSystemObject")
  [blue]etc.[/blue]

Either will work although late binding can take a bit longer. You should not notice a difference in a simple case like this.
 
Thanks for this good explanation.
I've learned something again...
 
I would just add to Golom's post - just to be clear:

[ul]
[li]Early binding DOES require reference to Microsoft Scripting Runtime[/li]
[li]Late binding DOES NOT require reference to Microsoft Scripting Runtime[/li]
[/ul]

Have fun.

---- Andy
 
And to be even clearer, and assuming a reference to the library exists:

Dim fso as filesystemobject
Set fso = createobject("scripting.filesystemobject")

is not late binding

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top