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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open files with same extension from a specific folder

Status
Not open for further replies.

jobur

Technical User
Aug 30, 2005
6
GB
Hi
I am trying to open all the files with a known extension (.prt This are normal txt file) in a specific folder.
I have searched the web site but was unsuccessful in finding what I am looking for. The BV code i have got is the following
-------------------
Sub OpenReportFile()

Dim fs, f, fc, ReportFile
Dim FileLocation As String

FileLocation = "J:\users\reportfile\"

Set fs = CreateObject("Scripting.FileSystemsObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files

For Each ReportFile In fc

If Right(ReportFile.Name, 4) = ".prt" Then

Workbooks.OpenText Filename:=FileLocation&ReportFile.Name, _
Origin:=xlWindows, _
StartRow:=1, _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=True, Semicolon:=False, _
Comma:=False, Space:=True, Other:=False, _
FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1))

End If
Next

End Sub

The macro crashes out with "ActivX component can't create object". It's something to do with SET fs!

Any ideas?

Thanks

Regards

JB
 
JB,

You may have a typo here:
Code:
Set fs = CreateObject("Scripting.FileSystem[b]s[/b]Object")
Should be:
Code:
Set fs = CreateObject("Scripting.FileSystemObject")

Regards,
Mike

 
...and should
Code:
fs.GetFolder(folderspec)
not be
Code:
fs.GetFolder(FileLocation)

what is folderspec? At that point in the code it appears to be nothing.

also, other than the string variable, all your variables are Variant.

As for not being able to create the object, uh, have you checked to see if you have a reference for the library that CAN create a scripting object?

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top