Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open Dialog Box

Status
Not open for further replies.

homeguard

IS-IT--Management
Jul 12, 2007
47
0
0
US
I am trying to make an open dialog box that when you click the button it prompts you to select a file, and then has a message box staying what file you selected and the file path. for some reason i cant find the code to make it display the file name and path.

Here is what i have:

Private Sub cmdOpen_Click()
'Requires reference to Microsoft Office 10.0 Object Library.

Dim fDialog As Office.FileDialog
Dim varFile As Variant

'Clear listbox contents.
'Me.FileList.RowSource = ""

'Set up the File Dialog.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
'Allow user to make multiple selections in dialog box
.AllowMultiSelect = False

'Set the title of the dialog box.
.Title = "Please select one file"

'Clear out the current filters, and add our own.
.Filters.Clear
.Filters.Add "All Files", "*.*"

'Show the dialog box. If the .Show method returns True, the
'user picked at least one file. If the .Show method returns
'False, the user clicked Cancel.
If .Show = True Then
'msgbox you selected this file and at this path.

Else

MsgBox "You clicked Cancel in the file dialog box."
End If
End With

End Sub

it doesnt seem that hard yet im having toubles with it.

thanks.
 
You are 99% there man - all you need to do is add this line before End With

Code:
MsgBox .SelectedItems.Item(1)


~Melagan
______
"It's never too late to become what you might have been.
 
Oops - my line should go after: If .Show = True Then


Incidently - if you wanted to allow multiple selections, something like this would do the trick.
Code:
Option Compare Database
Option Explicit
Private Sub cmdOpen_Click()
'Requires reference to Microsoft Office 10.0 Object Library.

    Dim fDialog As Office.FileDialog
    Dim varFile As Variant
    'Clear listbox contents.
    'Me.FileList.RowSource = ""

    'Set up the File Dialog.
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    With fDialog
        'Allow user to make multiple selections in dialog box
        .AllowMultiSelect = True
            
        'Set the title of the dialog box.
        .Title = "Please select one file"

        'Clear out the current filters, and add our own.
        .Filters.Clear
        .Filters.Add "All Files", "*.*"
      
        'Show the dialog box. If the .Show method returns True, the
        'user picked at least one file. If the .Show method returns
        'False, the user clicked Cancel.
        If .Show = True Then
            For Each varFile In .SelectedItems
                Debug.Print varFile
            Next
        Else
            MsgBox "You clicked Cancel in the file dialog box."
        End If
    End With
    Set fDialog = Nothing
    varFile = ""
    
 End Sub


~Melagan
______
"It's never too late to become what you might have been.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top