Routine shows list of PDF files in a windows folder but only to a certain file name length

neilshaw123

New Member
Joined
Jul 10, 2017
Messages
4
I have a piece of code to list PDF files that I am using from the answer on this thread Fill a combo box with filenames in a directory. It works until a certain condition is met; I have noticed that the files do not get picked up/shown when the file name length is beyond a certain length. The file that is not being picked up is as named as follows:

"Testfile GGGGG XXXX SSSSS AAAAAAAA aa Wwww Declaration form_XXXX1234.04.AUG16_31AUG16.pdf"

Does anyone have any ideas how to get around this?
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hello and welcome.

I can't see your thread you've linked to for some reason. Work Firewall not allowing it.

However I just tired using DIR with a directory containing the filename you have there and it lists the file no problem. What code are you using?
 
Upvote 0
Hello and welcome.

I can't see your thread you've linked to for some reason. Work Firewall not allowing it.

However I just tired using DIR with a directory containing the filename you have there and it lists the file no problem. What code are you using?


Here is the code I have based mine on:

Code:
[COLOR=blue]Option Explicit[/COLOR][COLOR=#333333][/COLOR] 
[COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] UserForm_Initialize() 
     [COLOR=darkgreen]'Files in folder listed in Listbox1[/COLOR]
    [COLOR=blue]Dim[/COLOR] FSO [COLOR=blue]As[/COLOR] [COLOR=blue]Object[/COLOR], fld [COLOR=blue]As[/COLOR] [COLOR=blue]Object[/COLOR], Fil [COLOR=blue]As[/COLOR] [COLOR=blue]Object[/COLOR] 
    [COLOR=blue]Dim[/COLOR] SubFolderName [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] 
    [COLOR=blue]Dim[/COLOR] i [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR] 
    [COLOR=blue]Set[/COLOR] FSO = CreateObject("Scripting.FileSystemObject") 
    Me.ListBox1.Clear [COLOR=darkgreen]'clear previous entries[/COLOR]
    SubFolderName = "C:\" 
    [COLOR=blue]Set[/COLOR] fld = FSO.GetFolder(SubFolderName) 
    [COLOR=blue]For Each[/COLOR] Fil [COLOR=blue]In[/COLOR] fld.Files 
        i = i + 1 
        Me.ListBox1.AddItem Fil.Name 
         
    [COLOR=blue]Next[/COLOR] Fil 
     
     
     
[COLOR=blue]End Sub[/COLOR]
 
Upvote 0
I used your code and it picked up the file no problem (although it picks up all files not just pdf). My Listbox was populated with all filenames from the directory. Out of interest, try this code which also works fine but without any late binding.

Code:
Private Sub UserForm_Initialize()
    Dim sPath As String
    Dim sFileName As String
    
    Me.ListBox1.Clear 'clear down Listbox
    
    sPath = "C:\" 'Change as required ensuring you have the final backslash '\'
    
    sFileName = Dir(sPath & "*.pdf") 'Get all pdf files from the path
    
    'loop through all files in the location
    Do While sFileName <> ""
        Me.ListBox1.AddItem sFileName
        sFileName = Dir 'Get next file
    Loop
End Sub
 
Upvote 0
Thank you for your reply gallen. I have just realised what the problem might be. It seems that there is a restriction on the length of the directory (perhaps over 256 characters). It turns out that I am unable to control where users place files so this has to stay in place.

In order to replicate this issue, create 2 directories as follows and place the file there, change your code to point to this directory instead:

C:\Testfile GGGGG XXXX SSSSS AAAAAAAA aa Wwww Declaration form_XXXX1234.04.AUG16_31AUG16\Testfile GGGGG XXXX SSSSS AAAAAAAA aa Wwww Declaration form_XXXX1234.04.AUG16_31AUG16\Testfile GGGGG XXXX SSSSS AAAAAAAA aa Wwww Declaration form_XXXX1234.04.AUG16_31AUG16.pdf

I am still at a loss though so any thoughts would be very much appreciated.

<tbody>
</tbody>
 
Last edited by a moderator:
Upvote 0
I still had no issues with your suggestion. I used this code:

Code:
Private Sub UserForm_Initialize()
    Dim sPath As String
    Dim sFileName As String
    
    Me.ListBox1.Clear 'clear down Listbox
    
    sPath = "C:\Test\Testfile GGGGG XXXX SSSSS AAAAAAAA aa Wwww Declaration form_XXXX1234.04.AUG16_31AUG16\" 'Change as required ensuring you have the final backslash '\'
    
    sFileName = Dir(sPath & "*.pdf") 'Get all pdf files from the path
    
    'loop through all files in the location
    Do While sFileName <> ""
        Me.ListBox1.AddItem sFileName
        sFileName = Dir 'Get next file
    Loop
End Sub
 
Upvote 0
Hi gallen,

Yours was successful because your file directory name length is not as per my example (over 255 characters), which is the upper acceptable limit in this regard. I have since found that a workaround to this is to use the DOS/shortened directory approach so where a file directory is long:

C:\Testfile GGGGG XXXX SSSSS AAAAAAAA aa Wwww Declaration form_XXXX1234.04.AUG16_31AUG16\Testfile GGGGG XXXX SSSSS AAAAAAAA aa Wwww Declaration form_XXXX1234.04.AUG16_31AUG16\Testfile GGGGG XXXX SSSSS AAAAAAAA aa Wwww Declaration form_XXXX1234.04.AUG16_31AUG16.pdf

You shorten it as per the following:

C:\Testfil~1\Testfile GGGGG XXXX SSSSS AAAAAAAA aa Wwww Declaration form_XXXX1234.04.AUG16_31AUG16\Testfile GGGGG XXXX SSSSS AAAAAAAA aa Wwww Declaration form_XXXX1234.04.AUG16_31AUG16.pdf


This isn't a 100% foolproof approach but it has gotten me out of jail free this time.
 
Upvote 0
Yes, Win 7 won't even let me create the file

C:\Testfile GGGGG XXXX SSSSS AAAAAAAA aa Wwww Declaration form_XXXX1234.04.AUG16_31AUG16\Testfile GGGGG XXXX SSSSS AAAAAAAA aa Wwww Declaration form_XXXX1234.04.AUG16_31AUG16\Testfile GGGGG XXXX SSSSS AAAAAAAA aa Wwww Declaration form_XXXX1234.04.AUG16_31AUG16.pdf

I get a message saying path too long. So I think your only answer is to shorten your file's/folder's name(s)
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,684
Members
449,116
Latest member
HypnoFant

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top