|
-
Sep 16th, 2004, 01:11 PM
#1
Thread Starter
Addicted Member
GetOpenFileName problem (resolved)
Hey All,
I found this code (GetOpenFileName API) that Aaron Young wrote
for multi-selecting files.
click here
This code works great for selecting multiple files, but when I want
to select just one, I can't seem to figure out why it will not work.
Thanks for any help in advance,
Ron
BTW...I use VB5, and I found this nice Split function (I don't
remember, but Aaron might have written this too)...
Code:
Private Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant
Dim sParts() As String
Dim lParts As Long
Dim lPos As Long
lPos = InStr(sString, sSeparator)
While lPos
ReDim Preserve sParts(lParts)
sParts(lParts) = Left(sString, lPos - 1)
sString = Mid(sString, lPos + Len(sSeparator))
lPos = InStr(sString, sSeparator)
lParts = lParts + 1
Wend
If Len(sString) Then
ReDim Preserve sParts(lParts)
sParts(lParts) = sString
End If
Split2 = IIf(lParts, sParts, Array())
End Function
Last edited by rdcody; Sep 16th, 2004 at 05:08 PM.
-
Sep 16th, 2004, 02:52 PM
#2
-
Sep 16th, 2004, 03:11 PM
#3
Thread Starter
Addicted Member
manavo11,
When I select one file only, and click the "open" button, it
will not add the file to the listbox. If I select several files, it
adds them perfectly.
Does it work for you if you select one file only?
-
Sep 16th, 2004, 03:24 PM
#4
Thread Starter
Addicted Member
This seems to work...
Dim OpenDialog As String
Code:
If lResult > 0 Then
With tOPENFILENAME
vFiles = Split2(Left(.lpstrFile, InStr(.lpstrFile, Chr(0) & Chr(0)) - 1), Chr(0))
End With
If UBound(vFiles) = 0 Then
List1.AddItem vFiles(0)
Else
For lIndex = 1 To UBound(vFiles)
List1.AddItem AddBS(vFiles(0)) & vFiles(lIndex)
Next
If List1.List(0) = "" Then
OpenDialog = Trim$(tOPENFILENAME.lpstrFile)
List1.AddItem OpenDialog
End If
End If
End If
-
Sep 16th, 2004, 03:27 PM
#5
Thread Starter
Addicted Member
Would someone check to see if this works for them?
Thanks,
Ron
-
Sep 16th, 2004, 03:46 PM
#6
VB Code:
If UBound(vFiles) = 0 Then
List1.AddItem vFiles(0)
This part of the code is supposed to run when only one file is selected...
Has someone helped you? Then you can Rate their helpful post. 
-
Sep 16th, 2004, 03:54 PM
#7
Thread Starter
Addicted Member
manavo11,
That's kinda what I thought, but it doesn't work for me. The code
above is the only way I could get a single file to work.
Does it work for you if you select one file only?
-
Sep 16th, 2004, 04:02 PM
#8
-
Sep 16th, 2004, 04:11 PM
#9
Thread Starter
Addicted Member
Well hell, I couldn't get yours to run because of the Split.
Apparently, you are using VB6, and do not have any problems
running the original code.
I use VB5, so I need to use the Split function I posted above.
I don't know what else it could be....do you?
-
Sep 16th, 2004, 04:26 PM
#10
-
Sep 16th, 2004, 04:51 PM
#11
That's the problem... When you put only one file, the array gets messed up! It returns an empty array and the UBound is -1. That's why it doesn't run But, with this split function, it works 
VB Code:
Private Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant
Dim sParts() As String
Dim lParts As Long
Dim lPos As Long
lPos = InStr(sString, sSeparator)
If lPos <> 0 Then
While lPos
ReDim Preserve sParts(lParts)
sParts(lParts) = Left(sString, lPos - 1)
sString = Mid(sString, lPos + Len(sSeparator))
lPos = InStr(sString, sSeparator)
lParts = lParts + 1
Wend
If Len(sString) Then
ReDim Preserve sParts(lParts)
sParts(lParts) = sString
End If
Split2 = IIf(lParts, sParts, Array())
Else
ReDim sParts(0)
sParts(0) = sString
Split2 = sParts
End If
End Function
Has someone helped you? Then you can Rate their helpful post. 
-
Sep 16th, 2004, 05:06 PM
#12
Thread Starter
Addicted Member
Hey manavo11...
Works excellent! I never would have figured it out. I really
appreciate all your help. Have a good one!
Thanks again,
Ron
-
Sep 16th, 2004, 05:12 PM
#13
-
Sep 17th, 2004, 02:30 AM
#14
Well when I went to sleep I thought of something... Check the bold line :
VB Code:
Private Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant
Dim sParts() As String
Dim lParts As Long
Dim lPos As Long
lPos = InStr(sString, sSeparator)
While lPos
ReDim Preserve sParts(lParts)
sParts(lParts) = Left(sString, lPos - 1)
sString = Mid(sString, lPos + Len(sSeparator))
lPos = InStr(sString, sSeparator)
lParts = lParts + 1
Wend
If Len(sString) Then
ReDim Preserve sParts(lParts)
sParts(lParts) = sString
[b]lParts = lParts + 1[/b]
End If
Split2 = IIf(lParts, sParts, Array())
End Function
I think that it should work now without all the fancy changes I made before
Has someone helped you? Then you can Rate their helpful post. 
-
Sep 17th, 2004, 09:59 AM
#15
Thread Starter
Addicted Member
Oh yeah.... it works great! I think I am beginning to understand
why it wouldn't work, and how you made it work. I always like
to learn new things.
Thanks again, and have a good one,
Ron
-
Sep 19th, 2004, 04:14 PM
#16
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|