Randomly select a file

prgwhiting

Registered User.
Local time
Today, 07:00
Joined
Apr 18, 2000
Messages
15
I'm sure this is really simple, but my mind is frying trying to solve this. I've a list of wav files in a folder located at D:\My Documents\Mail Alert Sounds\ . What I've set outlook to do is to play a sound located in this directory every time I receive an email. The tricky bit is, that I want to outlook to select a random email from those in the directory. Can anyone help before my head explodes.

Thanks
 
The way to do this is to read in all the wav files in the directory in question into an array using a loop. Next use the randomize function in access to generate a random number between 0 and the count of wav files. Use the random number generated by the randomize function as the array index to return a random wav file name.

I don't have any coding examples right now, but will try to whip something up and post another reply as an example. Good luck.
 
Here is some code to work with, I built this in Access VBA, but you should be able to use it....

Dim arrWavArray(1000) As String
Dim iCounter As Integer
Dim varFile As Variant
Dim fsoFileSearch As Object
Dim strRndmFile As String

'find all the files indicated
Set fsoFileSearch = Application.FileSearch
With fsoFileSearch
.NewSearch
'all .txt file extentsions
.Filename = "*.txt" 'or "*.Wav"
'directory c:\temp 'Your directory
.LookIn = "C:\Temp" 'your directory
'search all the subfolders
.SearchSubFolders = True
'run the search
.Execute

'add all the files to the array
iCounter = 0
For Each varFile In .FoundFiles
arrWavArray(iCounter) = varFile
iCounter = iCounter + 1
Next varFile
iCounter = iCounter - 1
End With

Randomize ' Initialize random-number generator.
strRndmFile = Int((iCounter * Rnd) + 0) 'generate random number

'find the corresponding array index, random file name
strRndmFile = arrWavArray(Int((iCounter * Rnd) + 0)) ' Generate random value between 1 and 6.

MsgBox strRndmFile
 

Users who are viewing this thread

Back
Top Bottom