Open File API call - multiple selections

G

Guest

Hi,

Using the API call to open the 'Select File' box, is it possible to accept
multiple file selections?

Cheers,
Steve,
 
D

Douglas J. Steele

Referring to the code in http://www.mvps.org/access/api/api0001.htm, first
you need to ensure that the Flag parameter includes the value specified by
ahtOFN_ALLOWMULTISELECT, something like:

lngFlags = ahtOFN_FILEMUSTEXIST Or _
ahtOFN_HIDEREADONLY Or _
ahtOFN_NOCHANGEDIR Or _
ahtOFN_ALLOWMULTISELECT

Once you've done that, the value passed back will consist of the folder
name, followed by a null character (Chr$(0)), followed by the name of the
first file selected, followed by a null character, followed by the name of
the second file selected, followed by a null character, and so on until the
last file. There will be two null characters at the end of the string.

Because of the null characters, you can't use the TrimNull function that's
included in the given sample. Instead, take off the last two characters:

ahtCommonFileOpenSave = Left$(OFN.strFile, Len(OFN.strFile)-2)

Then use the Split function to break the string into its component parts:

strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY) str

varFiles = Split(strInputFileName, Chr$(0))
strFolder = varFiles(0)
For intLoop = 1 to UBound(varFiles)
Debug.Print strFolder & "\" & varFiles(intLoop)
Next intLoop
 
G

Guest

Thanks for the reply.

I have had a try but cannot seem to get it to work.

Could you explain where I need to include the Flag Parameter please?

I have tried adding it the GetOpenFile Function in the lngFlags section.

Then I pasted the Split function into a new function and ran it. I got
presented with a 'funny' OpenFile dialog with a Directory 'Tree' on the right
hand side!

Cheers,
Steve.
 
D

Douglas J. Steele

Sorry: can you post your actual code here?

I just tested the following (which assumes you're using the code from
http://www.mvps.org/access/api/api0001.htm)

Sub djsTest()
Dim intLoop As Integer
Dim lngFlags As Long
Dim strFile As String
Dim strFolder As String
Dim strFilter As String
Dim strOutput As String
Dim varFiles As Variant

lngFlags = ahtOFN_FILEMUSTEXIST Or _
ahtOFN_HIDEREADONLY Or _
ahtOFN_NOCHANGEDIR Or _
ahtOFN_EXPLORER Or _
ahtOFN_ALLOWMULTISELECT
strFilter = ahtAddFilterItem(strFilter, "Access Files (*.mda, *.mdb)", _
"*.MDA;*.MDB")
strFilter = ahtAddFilterItem(strFilter, "dBASE Files (*.dbf)", "*.DBF")
strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)", "*.TXT")
strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")
strFile = ahtCommonFileOpenSave(InitialDir:="C:\", _
Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
DialogTitle:="Hello! Open Me!")

If Len(strFile) > 0 Then
strOutput = "You selected:" & vbCrLf
varFiles = Split(strFile, Chr$(0))
If UBound(varFiles) > 1 Then
strFolder = varFiles(0)
For intLoop = 1 To UBound(varFiles)
strOutput = strOutput & strFolder & varFiles(intLoop) &
vbCrLf
Next intLoop
Else
strOutput = strOutput & varFiles(0)
End If
Else
strOutput = "You didn't select anything"
End If
MsgBox strOutput

End Sub
 
G

Guest

I did get the code from the api0001.htm page.

I didnt realise where the lngFlags =..... part went.

I have tested it, but when I select more than 1 file, the Msgbox only says:
You Selected C:\. If I select only 1 file, it will show the path and
filename.

Any ideas?

Cheers,
Steve
 
N

Norman Goetz

I have tested it, but when I select more than 1 file, the Msgbox only says:
You Selected C:\. If I select only 1 file, it will show the path and
filename.

Any ideas?

Private Function TrimNull(ByVal strItem As String) As String
Dim intPos As Integer
intPos = InStr(strItem, vbNullChar & vbNullChar)
If intPos > 0 Then
TrimNull = Left(strItem, intPos - 1)
Else
TrimNull = strItem
End If
End Function

and change the djsTest-Function

Dim strFile As Variant 'String

hth

Norman Goetz

Norman Götz
 
D

Douglas J. Steele

Norman Goetz said:
Private Function TrimNull(ByVal strItem As String) As String
Dim intPos As Integer
intPos = InStr(strItem, vbNullChar & vbNullChar)
If intPos > 0 Then
TrimNull = Left(strItem, intPos - 1)
Else
TrimNull = strItem
End If
End Function

and change the djsTest-Function

Dim strFile As Variant 'String

There's no reason to change strFile to a variant: it works fine as a string.
And yes, your revised TrimNull function is probably safer than what I'd
recommended (simply removing the last 2 characters)
 
G

Guest

Norman/Douglas,

That works now cheers.

As a general question, if using a variant as an array, do you not need to
specify how many 'items' the array can hold? I was trying to use an array
where the maximum number of items was unknown but couldnt figure it out....

Cheers,
Steve.
 
G

Guest

OK, I have it working by calling a function and passing the Initial Dir and
Title to it.

In the old version I use to use, the function was pass the file name back to
the calling procedure. How do I pass the varFiles Array back to the calling
procedure?

I get an error 13 when I try GetFileNames = varFiles or an error 9 when I
try GetFileNames = varFiles()

Any ideas?

Cheers,
Steve.
 
D

Douglas J. Steele

I always pass back what the routine found as a string, and parse it in the
calling routine (or, more accurately, by passing what the function returned
to another routine that parses it).

Without seeing your actual code, I'm afraid I can't offer any suggestions.
If you're using the code from http://www.mvps.org/access/api/api0001.htm, I
don't see a GetFileNames anywhere on that page.
 

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