Cosmetic Problem: Default File Name Appears to be Truncated in File Dialog

sonic8

AWF VIP
Local time
Today, 20:16
Joined
Oct 27, 2015
Messages
1,184
Has someone a simple(!) workaround for the default file name appearing to be truncated (on the left) when it is displayed in a File Open Dialog?
2024-11-17_16h04_08.png

The full file name is: "test_default_file.txt"
This is also the actual content of the control, it just appears truncated. Only the rightmost 18 characters are displayed initially. If you put the focus into that text box and move the caret to the left the full file name appears.
So, the problem is "only" that users are confused because they think, the dialog does not pre-select the intended file name. This is aggravated by the fact that in practice the leftmost 8 characters of the file name are important to identify the correct file.

It doesn't make a difference whether I use the built-in Office file dialog or the GetOpenFileNameA API function.
This is a common problem and you find numerous mentions of it on the internet. However, I wasn't able to find a simple solution to it.

My code is bog standard and IMO mostly irrelevant here. For completeness sake nonetheless:
(Behavior is the same for msoFileDialogOpen)
Code:
Public Function SelectFileOffice(Optional ByVal InitialFileName As String = "") As String
 
    Dim fDialog As Office.FileDialog
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
   
    Dim fileName As String

    With fDialog
        .Filters.Add "All Files", "*.*"
     
        If InitialFileName > "" Then
            .InitialFileName = InitialFileName
        End If
         
        If .Show = True Then
            fileName = .SelectedItems(1)
        End If
    End With
   
    SelectFileOffice = fileName

End Function
Usage example for the Immediate Window: ? SelectFileOffice("c:\tmp\test_default_file.txt")

Of course, this is only a minor problem but it annoys me nonetheless.

It would probably work to use GetOpenFileNameA, define a hook proc and pass it in the lpfnHook field of the OPENFILENAMEA structure, and send some window message to the text box to make it display the full name. - However, implementing this would be a lot of work for such a "small" problem.
 
Perhaps try:
Code:
' ...
        If InitialFileName > "" Then
            .InitialFileName = Chr(34) & InitialFileName & Chr(34)
        End If
' ...
I know it shouldn't make any difference, but worth a try in the absence of any more concrete suggestions!
Guilty of not reading everything thoroughly in the first place 😖 😬
 
Try expanding the dialog so that the File name textbox is larger - it should remember the size for the next time too.
 
I know it shouldn't make any difference, but worth a try in the absence of any more concrete suggestions!
Guilty of not reading everything thoroughly in the first place 😖 😬
Not sure what you feel guilty about.
It actually makes a difference. While the initial directory is still set, there is no file name pre-selected at all. - So, this difference does not really help.
 
Try expanding the dialog so that the File name textbox is larger - it should remember the size for the next time too.
The size of the dialog and text box is mostly irrelevant to the problem. Only when the dialog is displayed at full-screen size the file name is displayed in full.
I personally dislike it when a file dialog is opened full size. But this is also irrelevant, as its not me who is using this but external users. So, this would be something the user must do. I'm not sure if this is even practical enough to suggest it.
 
I've never seen this behaviour.
You can copy, paste, and run the function provided in the original post to see for yourself.

Is it a Win11 thing?
No. As per my research, it started with Vista and affects every Windows version since.
I see this on Windows 10.

Does it happen when invoked from other programs?
As far as VBA in other MS Office programs is concerned, yes it does.
The other mentions of this problem I found during my research indicate that it happens with every program.
 
Yes, I see it too when using your code - I suppose I have never seen it before since I rarely specify a file name when using the FileDialog (usually just a folder path for InitialFileName).

Probably some artefact from before when windows file names were restricted in length!
 
Hi Phil,

I can reproduce the behaviour here too with your code and wasn't aware of this and wonder that no customer ever complained.

All I can provide is that I use GetOpenFileNameW to support unicode, but the behaviour is the same.

Really odd...
 
Last edited:
Yes, I see it too when using your code - I suppose I have never seen it before since I rarely specify a file name when using the FileDialog (usually just a folder path for InitialFileName).
Indeed, this is rather unusual. I don't think I ever tried to do that before.
In the specific case of this scenario, it is very likely that the user wants to import the same file again and again. - It's an Excel file that gets updated periodically. - So, I thought it would be convenient for the user to have the file name already set in the file dialog.

Probably some artefact from before when windows file names were restricted in length!
The 18 characters, which are displayed, don't match the historic 8+3 file name scheme. I rather think, it is an artifact from the time when the file dialog was that tiny, non-resizable box.
 
Of course, this is only a minor problem but it annoys me nonetheless.
This is not a fix, but I am also not sure how good of a workaround this would be.
Code:
    With fDialog
        .Filters.Add "Text Files", "*.txt" 'added a specific filter before all files filter
        .Filters.Add "All Files", "*.*"
   
        If InitialFileName > "" Then
            .InitialFileName = InitialFileName
        End If
       
        If .Show = True Then
            fileName = .SelectedItems(1)
        End If
    End With
1731892631079.png

Just a thought...

PS. Although I can now see the full file name; somehow, the file extension is not displayed. However, I can verify that the file extension of the selected file is returned by the function (even if it's the same one set as the initial filename and the user simply clicks Open in the file dialog.
 
Last edited:
That still happens if the file extension is not one of those specified.
I was using .png yesterday, just because it was a short name.
Just tried with you extra filter and I do get to see the full name plus the extension. I laways have view extensions on my computers (old school :0 )
Strangely enought the filter order is this All files first, unlike yours. ? :unsure:

1731919550646.png
 
Although I can now see the full file name; somehow, the file extension is not displayed.
Nice try, but the latter causally determines the former in this particular case only.
Because the extension is no longer in the main file name textbox, the file name is 4 characters shorter than before and thus you now see the 3 first characters, which were previously visually truncated.
If the file name is 4 or more characters longer than in my example, the problem still persists as before.

Just tried with you extra filter and I do get to see the full name plus the extension.
In the file type dropdown the extensions are in the same order as the filters were added. Only if the topmost extension in the dropdown matches the extension of the default file name, it is omitted from the file name text box.

When experimenting with this, keep in mind that the filters/extensions are cached between calls. You must add .Filters.Clear to start with a clean slate every time.
 

Users who are viewing this thread

Back
Top Bottom