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

Find files in a directory

Status
Not open for further replies.

bobbybobbertson

Programmer
Nov 9, 2001
119
0
0
US
I am not a programmer, but am trying to write a macro.

Is there as VB function to find all the files in a directory and put them in a variable (array) that I can then loop through?

I think this would be easy for a programmer. Can you help with the code?

1) Find .ppt files in directory
(subdirectories as well would be nice)
2) begin loop
3) open file
4) ActivePresentation.Slides.InsertFromFile "myfile"
5) close file
6) end loop
 
Take a look at the FileSearch object.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
found this. Thanks

Sub insert()
'
' Macro recorded 10/18/2004 by myname
'
ActivePresentation.ApplyTemplate "C:\Documents and Settings\myname\Desktop\delete\AMR Leased Line Backup -- Overview and Definition.ppt"

Dim strFileSearch() As Variant
Dim intFileCount As Integer
Set fs = Application.FileSearch
With fs
.LookIn = "C:\Documents and Settings\myname\Desktop\delete" ' StrDirectory_To_Search_For_files
.FileName = "*.ppt" 'type of file to search for
.SearchSubFolders = True
If .Execute > 0 Then

intFileCount = .FoundFiles.Count
ReDim strFileSearch(intFileCount)

For i = .FoundFiles.Count To 1 Step -1
Debug.Print .FoundFiles.Count
strFileSearch(i) = .FoundFiles(i)
ActivePresentation.Slides.InsertFromFile .FoundFiles(i), 0, 1
Next i
Else
MsgBox "There were no files found", vbInformation + vbOKOnly
End If
End With

End Sub
 
You could also use FileSystemObject. You must make a Reference to Microsoft Scripting Runtime. What version are you using? Because the following pulls ALL the sildes, from ALL the .PPT files. Not just the first slide. XP (2002)
Code:
Sub InsertAllSlides()
Dim fso As Scripting.FileSystemObject
Dim fld As Scripting.Folder
Dim fil As Scripting.File

Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder("c:\temp")

For Each fil In fld.Files
If Right(fil.ShortName, 3) = UCase("ppt") Then
 ActivePresentation.Slides.InsertFromFile _
   fil.Path, 0, 1
End If
Next
Set fld = Nothing
Set fso = Nothing
End Sub

Gerry
 
fumei, you have to write a recursive function if you want to scan the subdirectories too.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Yeah, I just kept to one folder. I didn't notice it was specified sub folders as well. If needed, I will post code, but I will leave it at this for now. See what happens. Hopefully bobbbybobby will take it from here.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top