0

It's been years since I've used Visual Basic. I downgraded from 2017 to 2010 (The version I was using while I was in school). I figured VB would be the best way to attempt a solution. (Although I'm sure there are other languages that would do it as well.)

I'm looking to get back into programming. Let me get to the problem.

My friend has an ever growing amount of text documents in a folder, and he wants a program to choose one at random, and open it.

I thought I'd put a TextBox with a Button that would let him open the folder where he stores his files. Then this program would read the number of text files in that folder, and randomly generate a number between one and that number, select, and open the document with its default program (if it's text, notepad; if it's DocX then word.)

I've been sitting at a blinking cursor for 45 minutes. I've gone on YouTube for help with this project.

Any advice, or help you guys can give me? Does this need to be simplified?

Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
Phrosty
  • 3
  • 1

2 Answers2

1

That sounds like a reasonable strategy to me.

It might be worth displaying some sort of progress to the user, say by putting the name of current file name being read into the status bar, in case there's a long delay reading the file names due to the large number of files in the folder, and/or a slow-running network drive. If you do this, remember to put a DoEvents into your loop to allow screen updates to display.

There's a separate thread on how to open files in their native handler here.

Hope this helps - good luck!

Belladonna
  • 161
  • 1
  • 10
0
Option Explicit
Public oFSO As Object
Public arrFiles()
Public lngFiles As Long
Sub Main()
    Dim sPath As String
    sPath = InputBox("Enter folder path", "Folder path")
    ' clear starting point
    lngFiles = 0
    Erase arrFiles

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Call recurse(sPath)

    Randomize
    Dim lngRandomFileNumber As Long
    lngRandomFileNumber = CLng(lngFiles * Rnd) + 1

    MsgBox "This is random file, that will be opened: " & arrFiles(lngRandomFileNumber)
    Call CreateObject("Shell.Application").Open(arrFiles(lngRandomFileNumber))
End Sub
Sub recurse(sPath As String)
    Dim oFolder As Object
    Dim oSubFolder As Object
    Dim oFile As Object

    Set oFolder = oFSO.GetFolder(sPath)

    'Collect file information
    For Each oFile In oFolder.Files
        lngFiles = lngFiles + 1
        ReDim Preserve arrFiles(lngFiles + 1)
        arrFiles(lngFiles) = sPath & "\" & oFile.Name

    Next oFile

    'looking for all subfolders
    For Each oSubFolder In oFolder.SubFolders
    'recursive call
    Call recurse(oSubFolder.path)
    Next oSubFolder
End Sub

You can paste this code in any VBA supporting application (MS Access, MS Excel, MS Word), call VBA editor (Shift + F11) and paste this code. After that press F5 and select Main() function. You'll see prompt to enter folder path, and after that you would get random file path.

I think it should be understandable in practice to see what program do

Updated: @Belladonna mentioned it clearly, to open file in default program.

NB: This code is passes through subfolders also, if you want to exclude subfolders, you should comment the recursive call block in recurce function

Van Ng
  • 773
  • 1
  • 7
  • 17