Open windows explorer on a CD from a form button

I need the VB code to make it so that I can create an "Explore the CD" button.  I just need to open up the CD in windows explorer for users to be able to browse through the files on it.  I have found things like this:


Shell ("D:" & "\explorer.exe")

but it did not work.  On top of that, I would like to not hard code the CD drive letter, and rather just have it say something like this:


"Open CDROM with explorer.exe"


Who is Participating?
This code will find the cd-rom letter for you:
And then you can
shell ("start " & strCDLetter)
to bring up the explorer window for it...
Private Sub Command1_Click()
Dim oFSO        As Scripting.FileSystemObject
Dim oDrive      As Scripting.Drive
    Set oFSO = New Scripting.FileSystemObject
    For Each oDrive In oFSO.Drives
        If oDrive.DriveType = CDRom Then
            If oDrive.IsReady Then
                Call Shell("explorer.exe " & oDrive.DriveLetter & ":\", vbNormalFocus)
            End If
        End If
    Set oFSO = Nothing
End Sub
You will need reference to Microsoft Scripting runtime
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Anjinsan5Author Commented:
sirbounty - tried your solution, but it gives an error "file not found" on this line:

Shell ("start" & justonedrive$)

detected the drive letter just fine, but won't open up windows explorer.

u can use:

shell("explorer g:") for example.

i.e shell ("explorer " & strCDLetter)
Anjinsan5Author Commented:
thanks agj, that seems to have worked, now my only question is how to make sure that the explorer window has focus when it opens...
Anjinsan5Author Commented:
never mind, i got it.  Simply used the info above and wrote:

Call Shell("explorer " & justonedrive$, vbNormalFocus)

works like a charm...thanks so much everybody!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.