Sub FileSearch()
'set current directory as the one to search
CurrentDirectory = "Z:\Singapore\Singapore Turf\Singapore Turf Club 2\QEII Cup 2006\QEII Cup 2006_VD\Millenium Copthorne International\TVGI Race Name Text\"
CurrentDirectory = "Z:\Singapore\Singapore Turf\Singapore Turf Club 2\QEII Cup 2006\QEII Cup 2006_VD\Millenium Copthorne International\TVGI Race Name Logo\"
'look in current directory and count the number of jpg files
With Application.FileSearch
.LookIn = CurrentDirectory
.Filename = "*.jpg"
.SearchSubFolders = False
If .Execute() > 0 Then
Randomize
MessageText = "There were " & .FoundFiles.Count & " file(s) found."
Choice = MsgBox(MessageText, vbOKCancel)
If Choice = vbCancel Then GoTo leave
' For i = 1 To .FoundFiles.Count
rndValue = Int((.FoundFiles.Count * Rnd) + 1)
Call InsertPictureInRange(.FoundFiles(rndValue), Range("B5:D10"))
Call InsertPictureInRange(.FoundFiles(rndValue), Range("J7:L11"))
' MsgBox .FoundFiles(i)
' Next i
MessageText = "There were " & .FoundFiles.Count & " file(s) found."
Choice = MsgBox(MessageText, vbOKCancel)
If Choice = vbCancel Then GoTo leave
For i = 1 To .FoundFiles.Count
Call TestInsertPictureInRange
MsgBox .FoundFiles(i)
Next i
Else
MsgBox "There were no files found.", vbCritical
End If
End With
leave:
End Sub
Sub TestInsertPictureInRange()
InsertPictureInRange "Z:\Singapore\Singapore Turf\Singapore Turf Club 2\QEII Cup 2006\QEII Cup 2006_VD\Millenium Copthorne International\TVGI Race Name Text\", _
Range("B5:D10")
InsertPictureInRange "Z:\Singapore\Singapore Turf\Singapore Turf Club 2\QEII Cup 2006\QEII Cup 2006_VD\Millenium Copthorne International\TVGI Race Name Logo\", _
Range("J7:L11")
End Sub
Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCells
t = .Top
l = .Left
w = .Offset(0, .Columns.Count).Left - .Left
h = .Offset(.Rows.Count, 0).Top - .Top
End With
' position picture
With p
.Top = t
.Left = l
.Width = w
.Height = h
End With
Set p = Nothing
End Sub