ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   User type not defined (https://www.excelbanter.com/excel-programming/328900-user-type-not-defined.html)

Steph[_3_]

User type not defined
 
Hello. I grabbed this bit of code a while ago off of the newsgroups, but
actually tried to use it today. When run, it gives me the Compile error:
User defined type not defined. Did I not copy a section of the code that I
needed? Thanks!

Sub Open_all_files() 'Opens all files in folder AND Subfolders

Dim FSO As Scripting.FileSystemObject
Dim TopFolder As String
Set FSO = New Scripting.FileSystemObject
TopFolder = "G:\G&A Test" '<<<<<<<<< CHANGE THIS TO TOP FOLDER
InnerProc FSO.GetFolder(TopFolder), FSO

End Sub

Sub InnerProc(F As Scripting.Folder, FSO As Scripting.FileSystemObject)

Dim SubFolder As Scripting.Folder
Dim OneFile As Scripting.File
Dim WB As Workbook

For Each SubFolder In F.SubFolders
InnerProc SubFolder, FSO
Next SubFolder
For Each OneFile In F.Files
Debug.Print OneFile.Path
If Right(OneFile.Name, 4) = ".xls" Then
Set WB = Workbooks.Open(Filename:=OneFile.Path)

'****Code that does stuff here*****

WB.Close savechanges:=True
End If
Next OneFile

End Sub



Jim Thomlinson[_3_]

User type not defined
 
You need to reference th project to

Microsoft Scripting Runtime

Tools - References in the VBE

HTH

"Steph" wrote:

Hello. I grabbed this bit of code a while ago off of the newsgroups, but
actually tried to use it today. When run, it gives me the Compile error:
User defined type not defined. Did I not copy a section of the code that I
needed? Thanks!

Sub Open_all_files() 'Opens all files in folder AND Subfolders

Dim FSO As Scripting.FileSystemObject
Dim TopFolder As String
Set FSO = New Scripting.FileSystemObject
TopFolder = "G:\G&A Test" '<<<<<<<<< CHANGE THIS TO TOP FOLDER
InnerProc FSO.GetFolder(TopFolder), FSO

End Sub

Sub InnerProc(F As Scripting.Folder, FSO As Scripting.FileSystemObject)

Dim SubFolder As Scripting.Folder
Dim OneFile As Scripting.File
Dim WB As Workbook

For Each SubFolder In F.SubFolders
InnerProc SubFolder, FSO
Next SubFolder
For Each OneFile In F.Files
Debug.Print OneFile.Path
If Right(OneFile.Name, 4) = ".xls" Then
Set WB = Workbooks.Open(Filename:=OneFile.Path)

'****Code that does stuff here*****

WB.Close savechanges:=True
End If
Next OneFile

End Sub




Rob Bovey

User type not defined
 
Hi Steph,

You need to set a reference to the "Microsoft Scripting Runtime" using
the Tools/References menu in the Visual Basic Editor. This is the type
library that contains the FileSystemObject used in the code below.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm

"Steph" wrote in message
...
Hello. I grabbed this bit of code a while ago off of the newsgroups, but
actually tried to use it today. When run, it gives me the Compile error:
User defined type not defined. Did I not copy a section of the code that
I
needed? Thanks!

Sub Open_all_files() 'Opens all files in folder AND Subfolders

Dim FSO As Scripting.FileSystemObject
Dim TopFolder As String
Set FSO = New Scripting.FileSystemObject
TopFolder = "G:\G&A Test" '<<<<<<<<< CHANGE THIS TO TOP FOLDER
InnerProc FSO.GetFolder(TopFolder), FSO

End Sub

Sub InnerProc(F As Scripting.Folder, FSO As Scripting.FileSystemObject)

Dim SubFolder As Scripting.Folder
Dim OneFile As Scripting.File
Dim WB As Workbook

For Each SubFolder In F.SubFolders
InnerProc SubFolder, FSO
Next SubFolder
For Each OneFile In F.Files
Debug.Print OneFile.Path
If Right(OneFile.Name, 4) = ".xls" Then
Set WB = Workbooks.Open(Filename:=OneFile.Path)

'****Code that does stuff here*****

WB.Close savechanges:=True
End If
Next OneFile

End Sub






All times are GMT +1. The time now is 02:36 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com