VBA to Select Multiple Files when one is too long

  • Trying to use the code below. If I select two files for example, and one of them is longer than 255 characters (including path), Excel will crash. It ignores my error handler and just crashes. I cannot figure out a way to get past this. Any ideas?


    Code
    Dim vSelectFiles As Variant
    On Error GoTo EndLabel
    vSelectFiles = Application.GetOpenFilename(MultiSelect:=True)
    Exit Sub
    EndLabel:
    MsgBox "Check file name lengths"
  • Hello,


    Instead of creating an Array ...


    Have you tried to use a standard loop ...?


    HTH

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • I don't think I could use a standard loop in this case because I'm using the GetOpenFilename and it has to collect multiple files form the selection. Correct me if I'm wrong, but I don't see how.

  • Hello,


    Below is an example to adapt to your needs :



    Hope this will help

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • Thanks, but this looks like it will only loop through all files in a folder. I need the user to select a single file or multiple files, thus the need for GetOpenFilename. And that has to be a variant. Maybe I'm still missing something, but I don't see how this code applies to my situation.

  • In your initial post, you are referring to the limit of 255 characters (including path) ...


    Could you build your Array with the Files' Names ... without the path ...


    or test each element of your Array to get rid of the exceptions ...

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • Ok, but I'm still not getting past the fact that I need to use GetOpenFilename to have the user select the files they want. There is no other way that I know of to ask a user to select files on their system. Given that, this action alone is what fails when one of the filenames is too long. I can't "build an array" of filenames if I don't know what files they want to choose, and the only way to get that information is to use GetOpenFilename.

  • What about creating a UserForm which would list of all the ' candidate ' Files ... allowing your User to Select the ones he needs ...

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • Maybe this could be a starting point for you

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!