Results 1 to 16 of 16

Thread: GetOpenFileName problem (resolved)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Question 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.

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    What is the problem? It works fine for me


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154
    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?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154
    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154
    Would someone check to see if this works for them?

    Thanks,
    Ron

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    VB Code:
    1. If UBound(vFiles) = 0 Then
    2.             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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154
    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?

  8. #8
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Well, it runs correctly for me... And I couldn't get your version to work... Here is the project I have tested with. Maybe it'll work
    Attached Files Attached Files


    Has someone helped you? Then you can Rate their helpful post.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154
    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?

  10. #10
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    I forgot that! Then it must be the Split2 function that is causing the problem... I'll look and get back to you


    Has someone helped you? Then you can Rate their helpful post.

  11. #11
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    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:
    1. Private Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant
    2.     Dim sParts() As String
    3.     Dim lParts As Long
    4.     Dim lPos As Long
    5.    
    6.     lPos = InStr(sString, sSeparator)
    7.     If lPos <> 0 Then
    8.         While lPos
    9.             ReDim Preserve sParts(lParts)
    10.             sParts(lParts) = Left(sString, lPos - 1)
    11.             sString = Mid(sString, lPos + Len(sSeparator))
    12.             lPos = InStr(sString, sSeparator)
    13.             lParts = lParts + 1
    14.         Wend
    15.         If Len(sString) Then
    16.             ReDim Preserve sParts(lParts)
    17.             sParts(lParts) = sString
    18.         End If
    19.         Split2 = IIf(lParts, sParts, Array())
    20.     Else
    21.         ReDim sParts(0)
    22.         sParts(0) = sString
    23.         Split2 = sParts
    24.     End If
    25. End Function


    Has someone helped you? Then you can Rate their helpful post.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154
    Hey manavo11...

    Works excellent! I never would have figured it out. I really
    appreciate all your help. Have a good one!

    Thanks again,
    Ron

  13. #13
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    No problem

    Have fun


    Has someone helped you? Then you can Rate their helpful post.

  14. #14
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Well when I went to sleep I thought of something... Check the bold line :

    VB Code:
    1. Private Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant
    2.     Dim sParts() As String
    3.     Dim lParts As Long
    4.     Dim lPos As Long
    5.    
    6.     lPos = InStr(sString, sSeparator)
    7.     While lPos
    8.         ReDim Preserve sParts(lParts)
    9.         sParts(lParts) = Left(sString, lPos - 1)
    10.         sString = Mid(sString, lPos + Len(sSeparator))
    11.         lPos = InStr(sString, sSeparator)
    12.         lParts = lParts + 1
    13.     Wend
    14.     If Len(sString) Then
    15.         ReDim Preserve sParts(lParts)
    16.         sParts(lParts) = sString
    17.         [b]lParts = lParts + 1[/b]
    18.     End If
    19.     Split2 = IIf(lParts, sParts, Array())
    20. 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.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154
    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

  16. #16
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Originally posted by rdcody
    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


    Has someone helped you? Then you can Rate their helpful post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width