Link to home
Start Free TrialLog in
Avatar of David Everett
David EverettFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How do I open a Open a Windows folder with VBA?

Hi, I want to click a Button, and run some VBA code that will open a File Explorer Window.

For exmple:

On Form "F_Files" I have a button "btnOpenFiles".
On my PC I have a folder like "C:\AllClients\Company1"

What code will show the contents of the Folder "Company1"?

Thanks.
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

If you simple want to open the Folder then you will need something like this
Function OpenFolder()
Dim yourFolder As String
yourFolder  = "C:\AllClients\Company1 \"


Shell "C:\WINDOWS\explorer.exe """ & yourFolder  & "", vbNormalFocus
End Function

Open in new window

Now if you want to open the folder and pick some files then you will need a fileDialog

Public Function SelectFile(Optional DefaultPath As String) As String
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogOpen)
    With fd
    If Len(DefaultPath) > 0 Then .InitialFileName = ProperEndingForFolder(DefaultPath)
        .AllowMultiSelect = False
        .title = "Please select file to attach"
        If .Show = True Then
            SelectFile = .SelectedItems(1)
        Else
            Exit Function
        End If
    End With
    Set fd = Nothing
End Function


Public Function ProperEndingForFolder(strFolder) As String
If right(strFolder, 1) = "\" Then
ProperEndingForFolder = strFolder
Else
ProperEndingForFolder = strFolder & "\"
End If
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of David Everett

ASKER

Just what I need to know. Thanks.
Killer double quotes...  :)