Application.FileDialog(msoFileDialogOpen)

H

H.A. de Wilde

LS,

from Excel with VBA using a UserForm I can display the files in a
special directory, using:

With Application.FileDialog(msoFileDialogOpen)
..InitialFileName = strPath & "\*"
..AllowMultiSelect = False
..Show
End With

How do I manage that from that FileDiaolog a file (.xls, .doc or .txt)
can be opened?

Doubleclick on the file gives no result.
 
R

Ron de Bruin

This example use GetOpenFilename that can do the same and it is working in 97 and up

Sub test()
Dim FName As Variant
Dim wb As Workbook
Dim MyPath As String
Dim SaveDriveDir As String

SaveDriveDir = CurDir

MyPath = ThisWorkbook.Path
ChDrive MyPath
ChDir MyPath

FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xls), *.xls")
If FName <> False Then
Set wb = Workbooks.Open(FName)
MsgBox "your code"
wb.Close
End If

ChDrive SaveDriveDir
ChDir SaveDriveDir

End Sub


For multiselect do it like this

Sub testing()
Dim FName As Variant
Dim N As Long
FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xls), *.xls", _
MultiSelect:=True)

If IsArray(FName) Then
For N = LBound(FName) To UBound(FName)
Workbooks.Open (FName(N))
Next
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top