I'm just going over some old code from ages ago and came across the below code to show a random file from a specific folder.
I didn't really use it but now have a use for it. I lost my notes as this was from the old Woody's site and cannot for the life of me remember how to use the code.
The module is straight forward as it is already in my modUtils. I can't remember where to put the calling code or timer settings etc.
Would someone mind looking and advising? I know who wrote it but seeing a previous post would be rude to direct the request directly.
Posted 2006-10-28 02:24
Put this function in a module:
Code: Select all
Function SelectRandomFile( _
ByVal strFolder As String, _
ByVal strExt As String) As String
Dim strFile As String
Dim intCount As Integer
Dim arr() As String
Dim i As Integer
On Error GoTo ErrHandler
' Trailing backslash is obligatory
If Not Right(strFolder, 1) = "" Then
strFolder = strFolder & ""
End If
strFile = Dir(strFolder & "*." & strExt)
Do While Not strFile = ""
intCount = intCount + 1
ReDim Preserve arr(1 To intCount)
arr(intCount) = strFile
strFile = Dir
Loop
If intCount = 0 Then
Exit Function
End If
Randomize
i = Int(1 + Rnd * intCount)
strFile = arr(i)
SelectRandomFile = strFolder & strFile
ExitHandler:
Erase arr
Exit Function
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Function
Use like this:
Me.AcrobatPath = SelectRandomFile("L:MMPDFUtilities", "pdf")
Me.Pdf1.LoadFile Me.AcrobatPath