PDA

View Full Version : dialog picker question



saban
11-05-2007, 06:43 AM
How can I get filename and its path with Application.FileDialog(msoFileDialogFilePicker) something like this

So that file name and path will be stored in some variable or something so I can do something on this file

Thnx
saban

OTWarrior
11-05-2007, 07:47 AM
would this do?

Dim strDocName As String
strDocName = FindFile(Desktop, "Please Select file","*.doc", "*.doc;")

TonyJollans
11-05-2007, 08:22 AM
A quick Google suggests that FindFile is some kind of AddIn - it certainly isn't normal VBA.

Try ..

With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
If .Show = -1 Then
MsgBox .SelectedItems(1)
End If
End With


.. it should at least get you started

lucas
11-05-2007, 08:45 AM
This one works....from Gerry:
ps you can change the path to start your search for files in the sub test so that you can start in a certain directory if you want to.
Option Explicit
Private Declare Function GetOpenFileName _
Lib "comdlg32.dll" Alias "GetOpenFileNameA" _
(pOpenfilename As OpenFileName) As Long

Private Type OpenFileName
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Function BrowseForFile(sInitDir As String, _
Optional ByVal sFileFilters As String, _
Optional sTitle As String = "Open File", _
Optional lParentHwnd As Long) As String

Dim tFileBrowse As OpenFileName
Const clMaxLen As Long = 254

tFileBrowse.lStructSize = Len(tFileBrowse)

'Replace friendly deliminators with nulls
sFileFilters = Replace(sFileFilters, "|", vbNullChar)
sFileFilters = Replace(sFileFilters, ";", vbNullChar)
If Right$(sFileFilters, 1) <> vbNullChar Then
'Add final delimiter
sFileFilters = sFileFilters & vbNullChar
End If

'Select a filter
tFileBrowse.lpstrFilter = sFileFilters & _
"All Files (*.*)" & vbNullChar & "*.*" _
& vbNullChar
'create a buffer for the file
tFileBrowse.lpstrFile = String(clMaxLen, " ")
'set the maximum length of a returned file
tFileBrowse.nMaxFile = clMaxLen + 1
'Create a buffer for the file title
tFileBrowse.lpstrFileTitle = Space$(clMaxLen)
'Set the maximum length of a returned file title
tFileBrowse.nMaxFileTitle = clMaxLen + 1
'Set the initial directory
tFileBrowse.lpstrInitialDir = sInitDir
'Set the parent handle
tFileBrowse.hwndOwner = lParentHwnd
'Set the title
tFileBrowse.lpstrTitle = sTitle

'No flags
tFileBrowse.flags = 0

'Show the dialog
If GetOpenFileName(tFileBrowse) Then
BrowseForFile = Trim$(tFileBrowse.lpstrFile)
If Right$(BrowseForFile, 1) = vbNullChar Then
'Remove trailing null
BrowseForFile = Left$(BrowseForFile, _
Len(BrowseForFile) - 1)
End If
End If
End Function

Sub Test()
' this will return any file
MsgBox BrowseForFile("c:\")
' this will return only .xls files
' MsgBox BrowseForFile("c:\", "Excel File (*.xls);*.xls", _
' "Open Workbook")

End Sub

saban
11-05-2007, 08:52 AM
thnx guys this will do

I like that option: will try it first
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
If .Show = -1 Then
MsgBox .SelectedItems(1)
End If
End With


This one is fine it also disables OK button until file is selected

OTWarrior
11-05-2007, 08:53 AM
whoops...sorry about that, i copied it from some code that i used recently, but my colleague wrote his own function into it for some reason(he often does that and fails to tell me).