PC Review


Reply
Thread Tools Rate Thread

Code to open multiple files from within a folder

 
 
Karen
Guest
Posts: n/a
 
      17th Sep 2008
Hi all,

I'm having problems with this script below. I need some Excel VBA code that
will open multiple files from within a folder. Sometimes there maybe 5 files
to open, sometimes there may be more. I will never know how many files will
be in the folder each time I execute the code, and I won't know the file
names in the folder either.

The script below is nearly correct, however it seems to retain the file
names from the last time it was executed. I need a way of flushing it out so
it doesn't retain anything when it ends. If it retains the file names then it
expects them to be there when it is next executed, which of corse causes
errors.

Thanks for any help with this - regards Karen. The script I'm using is
pasted below.

With Application.FileSearch
.NewSearch
.LookIn = "C:\testfolder"
.SearchSubFolders = False
.Filename = "*.xls"
.MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks
If .Execute() > 0 Then
MsgBox "There were " & .FoundFiles.Count & " file(s) found."
For i = 1 To .FoundFiles.Count
Workbooks.Open Filename:=.FoundFiles(i)
MsgBox .FoundFiles(i)
Next i
End If
End With
 
Reply With Quote
 
 
 
 
anon
Guest
Posts: n/a
 
      17th Sep 2008
If you are simply looking to open every file within the folder that is
Excel try;

Dim objFSO As Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Dim objSubfolder As Scripting.Folder
Dim objFile As Scripting.File
Dim iRow As Long
Dim istr as string

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\test\")
For Each objFile In objFolder.files
If objFile.Type = "Microsoft Excel Worksheet" Then
istr = objFolder.Path & "\" & objFile.Name
Workbooks.Open Filename:=str
else
'do nothing
end if
next objfile
 
Reply With Quote
 
Karen
Guest
Posts: n/a
 
      17th Sep 2008
Thanks for your help anon - however is there someting else I need to include
as it comes up with the error message "User-defined type not defined" - on
the Dim variable declarations.

Thanks
Karen

"anon" wrote:

> If you are simply looking to open every file within the folder that is
> Excel try;
>
> Dim objFSO As Scripting.FileSystemObject
> Dim objFolder As Scripting.Folder
> Dim objSubfolder As Scripting.Folder
> Dim objFile As Scripting.File
> Dim iRow As Long
> Dim istr as string
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFolder = objFSO.GetFolder("C:\test\")
> For Each objFile In objFolder.files
> If objFile.Type = "Microsoft Excel Worksheet" Then
> istr = objFolder.Path & "\" & objFile.Name
> Workbooks.Open Filename:=str
> else
> 'do nothing
> end if
> next objfile
>

 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      17th Sep 2008
Try this variation

Dim objFSO As Object
Dim objFolder As Object
Dim objSubfolder As Object
Dim objFile As Object
Dim iRow As Long
Dim istr as string

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\test\")
For Each objFile In objFolder.files
If objFile.Type Like "Microsoft*Excel*Worksheet*" Then
istr = objFolder.Path & "\" & objFile.Name
Workbooks.Open Filename:=str
else
'do nothing
end if
next objfile

--
__________________________________
HTH

Bob

"Karen" <(E-Mail Removed)> wrote in message
news:321351F7-14B4-4095-8F6B-(E-Mail Removed)...
> Thanks for your help anon - however is there someting else I need to
> include
> as it comes up with the error message "User-defined type not defined" - on
> the Dim variable declarations.
>
> Thanks
> Karen
>
> "anon" wrote:
>
>> If you are simply looking to open every file within the folder that is
>> Excel try;
>>
>> Dim objFSO As Scripting.FileSystemObject
>> Dim objFolder As Scripting.Folder
>> Dim objSubfolder As Scripting.Folder
>> Dim objFile As Scripting.File
>> Dim iRow As Long
>> Dim istr as string
>>
>> Set objFSO = CreateObject("Scripting.FileSystemObject")
>> Set objFolder = objFSO.GetFolder("C:\test\")
>> For Each objFile In objFolder.files
>> If objFile.Type = "Microsoft Excel Worksheet" Then
>> istr = objFolder.Path & "\" & objFile.Name
>> Workbooks.Open Filename:=str
>> else
>> 'do nothing
>> end if
>> next objfile
>>



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      17th Sep 2008
Try changing:

Dim objFSO As Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Dim objSubfolder As Scripting.Folder
Dim objFile As Scripting.File

to

Dim objFSO As Object
Dim objFolder As Object
Dim objSubfolder As Object
Dim objFile As Object

======
An alternative is to set a reference (tools|references) to
Microsoft Scripting Runtime



Karen wrote:
>
> Thanks for your help anon - however is there someting else I need to include
> as it comes up with the error message "User-defined type not defined" - on
> the Dim variable declarations.
>
> Thanks
> Karen
>
> "anon" wrote:
>
> > If you are simply looking to open every file within the folder that is
> > Excel try;
> >
> > Dim objFSO As Scripting.FileSystemObject
> > Dim objFolder As Scripting.Folder
> > Dim objSubfolder As Scripting.Folder
> > Dim objFile As Scripting.File
> > Dim iRow As Long
> > Dim istr as string
> >
> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > Set objFolder = objFSO.GetFolder("C:\test\")
> > For Each objFile In objFolder.files
> > If objFile.Type = "Microsoft Excel Worksheet" Then
> > istr = objFolder.Path & "\" & objFile.Name
> > Workbooks.Open Filename:=str
> > else
> > 'do nothing
> > end if
> > next objfile
> >


--

Dave Peterson
 
Reply With Quote
 
Karen
Guest
Posts: n/a
 
      17th Sep 2008
Thanks for your help guys.

Kind regards
Karen

"Karen" wrote:

> Hi all,
>
> I'm having problems with this script below. I need some Excel VBA code that
> will open multiple files from within a folder. Sometimes there maybe 5 files
> to open, sometimes there may be more. I will never know how many files will
> be in the folder each time I execute the code, and I won't know the file
> names in the folder either.
>
> The script below is nearly correct, however it seems to retain the file
> names from the last time it was executed. I need a way of flushing it out so
> it doesn't retain anything when it ends. If it retains the file names then it
> expects them to be there when it is next executed, which of corse causes
> errors.
>
> Thanks for any help with this - regards Karen. The script I'm using is
> pasted below.
>
> With Application.FileSearch
> .NewSearch
> .LookIn = "C:\testfolder"
> .SearchSubFolders = False
> .Filename = "*.xls"
> .MatchTextExactly = True
> .FileType = msoFileTypeExcelWorkbooks
> If .Execute() > 0 Then
> MsgBox "There were " & .FoundFiles.Count & " file(s) found."
> For i = 1 To .FoundFiles.Count
> Workbooks.Open Filename:=.FoundFiles(i)
> MsgBox .FoundFiles(i)
> Next i
> End If
> End With

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual Basic Code to open a folder in Code builder =?Utf-8?B?RWJpdGFyaQ==?= Microsoft Access 0 28th Jun 2007 02:00 PM
User selection of folder and open all .xls files within folder =?Utf-8?B?QmFyYiBSZWluaGFyZHQ=?= Microsoft Excel Programming 4 14th Apr 2007 01:41 PM
Open multiple files into multiple worksheets of the same workbook =?Utf-8?B?bGlsIE1hdHQ=?= Microsoft Excel Misc 1 31st Oct 2006 09:03 PM
Modify macro code to export multiple cell contents to multiple Text Files todk1@yahoo.com Microsoft Excel Programming 3 14th Oct 2006 08:26 AM
powerpoint won't open multiple files in multiple windows =?Utf-8?B?dHlrZWxz?= Microsoft Powerpoint 2 6th Oct 2006 03:44 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:17 AM.