recursive VBA code?

G

Geoff Cox

Hello,

Can anyone please point me at VBA code for moving recursively through
all the ppt files in a series of sub-folders?

Cheers

Geoff
 
J

Jonathan West

Geoff Cox said:
Hello,

Can anyone please point me at VBA code for moving recursively through
all the ppt files in a series of sub-folders?

Look up the FileSearch object in VBA Help. To search a folder hierarchy, set
the SearchSubfolders property to True


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
G

Geoff Cox

Look up the FileSearch object in VBA Help. To search a folder hierarchy, set
the SearchSubfolders property to True

Jonathan,

Thanks for your reply.

Whilst I am doing the above I came across the code beow which lists
all the files in sub-folders.

The

Debug.Print varItem

line below prints out each file name and its path in a series of
sub-folders.

I wonder if you could show me how I could use this info in varItem to
call a sub-routine which would act on each file?

Cheers

Geoff


Sub TestGetFiles()
' Call to test GetFiles function.

Dim dctDict As Scripting.Dictionary
Dim varItem As Variant
Dim strDirPath As String

strDirPath = "c:\a-temp1\"
' Create new dictionary.
Set dctDict = New Scripting.Dictionary
' Call recursively, return files into Dictionary object.
If GetFiles(strDirPath, dctDict, True) Then
' Print items in dictionary.
For Each varItem In dctDict
Debug.Print varItem


Next
End If
End Sub

Function GetFiles(strPath As String, _
dctDict As Scripting.Dictionary, _
Optional blnRecursive As Boolean) As Boolean

' This procedure returns all the files in a directory into
' a Dictionary object. If called recursively, it also returns
' all files in subfolders.

Dim fsoSysObj As Scripting.FileSystemObject
Dim fdrFolder As Scripting.Folder
Dim fdrSubFolder As Scripting.Folder
Dim filFile As Scripting.File

' Return new FileSystemObject.
Set fsoSysObj = New Scripting.FileSystemObject

On Error Resume Next
' Get folder.
Set fdrFolder = fsoSysObj.GetFolder(strPath)
If Err <> 0 Then
' Incorrect path.
GetFiles = False
GoTo GetFiles_End
End If
On Error GoTo 0

' Loop through Files collection, adding to dictionary.
For Each filFile In fdrFolder.Files
dctDict.Add filFile.Path, filFile.Path
Next filFile

' If Recursive flag is true, call recursively.
If blnRecursive Then
For Each fdrSubFolder In fdrFolder.SubFolders
GetFiles fdrSubFolder.Path, dctDict, True
Next fdrSubFolder
End If

' Return True if no error occurred.
GetFiles = True

GetFiles_End:
Exit Function
End Function

' Return True if no error occurred.
''GetFiles = True

''GetFiles_End:
''Exit Function
''End Function


Sub MyMacro(strMyFile As String)
' this gets called once for each file that meets the spec you enter in
ForEachPresentation
' strMyFile is set to the file name each time

' Probably at a minimum, you'd want to:
Dim oPresentation As Presentation
Set oPresentation = Presentations.Open(strMyFile)

With oPresentation


Dim oSh As shape
Dim bFoundButton As Boolean
Dim bFoundRectangle As Boolean

bFoundButton = False ' to start

With ActivePresentation.Slides(ActivePresentation.Slides.Count)

If .Shapes.Count > 0 Then

For Each oSh In _
ActivePresentation.Slides(ActivePresentation.Slides.Count) _
.Shapes
If oSh.Type = 1 Then
If oSh.AutoShapeType = 130 Then
bFoundButton = True
End If
End If
Next

' Now display a message
' If bFoundButton Then
' MsgBox "Found a Forward or Next button on the last slide"
'Else
' MsgBox "No Forward/Next buttons here"
'End If

If Not bFoundButton Then
MsgBox "No button on last slide of " & strMyFile
End If

Else
MsgBox "No shape on this last slide in " & strMyFile
End If

End With

oPresentation.Close


End With
Set oSh = Nothing
Set oPresentation = Nothing

End Sub
 
G

Geoff Cox

Look up the FileSearch object in VBA Help. To search a folder hierarchy, set
the SearchSubfolders property to True

Jonathan,

I can use the code below - based on your suggestion above. This gives
the name of each ppt file - hwo could I run a sub-routine on each of
these files?

Geoff

Sub search_subfolders()

Set fs = Application.FileSearch
With fs
.LookIn = "C:\a-temp1"
.SearchSubFolders = True
.FileName = "*.ppt"
If .Execute() > 0 Then
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found."
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)
Next i
Else
MsgBox "There were no files found."
End If
End With

End Sub
 
G

Geoff Cox

Look up the FileSearch object in VBA Help. To search a folder hierarchy, set
the SearchSubfolders property to True

Jonathan,

I have surprised myself!

The code below checks each file to see if there is an action button on
the last slide.

Thanks for your help - if you see anything wrong please let me know!

Cheers

Geoff

Sub search_subfolders()

Set fs = Application.FileSearch
With fs
.LookIn = "C:\a-temp2"
.SearchSubFolders = True
.FileName = "*.ppt"
If .Execute() > 0 Then

For i = 1 To .FoundFiles.Count
check_for_button_lastslide (.FoundFiles(i))


Next i
Else
MsgBox "There were no files found."
End If
End With

End Sub

Sub check_for_button_lastslide(strMyFile As String)

Dim oPresentation As Presentation
Set oPresentation = Presentations.Open(strMyFile)

With oPresentation


Dim oSh As shape
Dim bFoundButton As Boolean
Dim bFoundRectangle As Boolean

bFoundButton = False ' to start

With ActivePresentation.Slides(ActivePresentation.Slides.Count)

If .Shapes.Count > 0 Then

For Each oSh In _
ActivePresentation.Slides(ActivePresentation.Slides.Count) _
.Shapes
If oSh.Type = 1 Then
If oSh.AutoShapeType = 130 Then
bFoundButton = True
End If
End If
Next


If Not bFoundButton Then
MsgBox "No button on last slide of " & strMyFile
End If

Else
MsgBox "No shape on this last slide in " & strMyFile
End If

End With

oPresentation.Close


End With
Set oSh = Nothing
Set oPresentation = Nothing

End Sub
 
J

Jonathan West

Geoff Cox said:
Jonathan,

I have surprised myself!

The code below checks each file to see if there is an action button on
the last slide.

Thanks for your help - if you see anything wrong please let me know!


It looks like you have got it pretty sorted.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
G

Geoff Cox

It looks like you have got it pretty sorted.

Jonathan,

Thanks again for pointing me in right direction! This has saved me a
lot of tedious work.

Cheers

Geoff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top