InitialFileName of FileDialog doesn't display whole file name - excel

So, I'm opening a FileDialog from a workbook that lets the user choose and import files. I have a bit of logic that recommends a specific directory and file via .InitialFileName, depending on the criteria set by the user. This code handles the FileDialog:
With objFileDialog
.InitialFileName = strIFN 'This is the relevant line
.AllowMultiSelect = False
.ButtonName = "Select"
.Title = "Please select the file containing " & whichFile
If .Show > 0 Then
End If
If (.SelectedItems.Count > 0) Then
strPath = .SelectedItems(1)
End If
End With
strIFN contains the path of the recommended file, for example:
\\Company-Server\Users\Username\Desktop\Intern Unterlagen\Projektcontrolling\Testlauf\AK\201909_Company_Zeiteinträge_AK.xlsx
The path works fine, but in the opened FileDialog I see this:
As you can see, the file name is cut short by a weird scroll setting. The box does in fact contain the whole file name, but doesn't show it until you click into it and scroll to the left. As this confuses the user, I am trying to display the whole file name. I'd appreciate any tips on this.
As a bonus I would ideally want to already select the recommended file (having it highlighted in blue), but that is not essential for usability.

This appears to be the normal behavior, I can replicate it pretty easily in Excel 2016. While SendKeys is usually frowned upon, it seems to be useful for this case:
With objFileDialog
.InitialFileName = strIFN 'This is the relevant line
.AllowMultiSelect = False
.ButtonName = "Select"
.Title = "Please select the file containing " & whichFile
On Error Resume Next
SendKeys "{HOME}"
On Error GoTo 0
If .Show > 0 Then
strPath = .SelectedItems(1)
End If
End With
When the dialog is displayed, the text cursor is at the end of the filename, and the text box containing the filename has focus. So, this was a bit of a shot in the dark, but I figured that "{HOME}" ought to return the cursor to the beginning of the filename, just like it would if the user hit the HOME key with the dialog open.
Note: In my observation, it seems to make no difference whether you include Wait:=True to SendKeys.

Related

Office.FileDialog not performing as I expected

I gave up on named ranges, switching to the built-in import from Excell tool. When it comes to selecting the spreadsheet to import, the open file dialog won't work. Yes, I added the required reference, when prompted.
According to all the documentation I have read, this should work:
Dim FilePath As Variant
Dim dlg As Office.FileDialog
Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
With dlg
.AllowMultiSelect = False
.Title = "Select Log Sheet"
.Filters.Clear
.Filters.Add "Select Log Sheet", "*.xlsx"
If .Show = True Then
FilePath = dlg.SelectedItems
Else
MsgBox "You Clicked Cancel"
End If
End With
however, when it gets to .Filters.Add, I get
"Object doesn't support the property or method"
I'm starting to think Access hates me. I came out of retirement to write a quick log sheet processing app and the struggle with Access is getting annoying.
Thanks for any help. By the way, the next step is to feed the returned file to the saved import, and so far I've not seen how to pass a file argument to it.
You are using msoFileDialogFolderPicker dialog,
This is for selecting a folder (not a file) and thus doesn't support filtering of file type (as you are attempting to do).
Suggest using msoFileDialogFilePicker instead. This is for getting a target file path (which is what your code implies you're after).
Separately, you have a second bug and can trim your code a tad (as shown below).
Note the use of .SelectedItems(1) and doing away with setting a dialog object:
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Select Log Sheet"
.Filters.Clear
.Filters.Add "Select Log Sheet", "*.xlsx"
If .Show = True Then
FilePath = .SelectedItems(1)
Else
MsgBox "You Clicked Cancel"
End If
End With

How do I import Excel data using dynamic path created/Input on Access Form

In MS Access, I created a form with Form Name "Form" and created a Text Box named "Path".
When I enter a path in form with "c:Desktop.....xlsx". I should be able to import data from Excel to Access.
I tried using the Macro version in Access using the below code, but I encounter an error.
Function Import()
On Error GoTo Import_Err
Dim Str As String
Str = Forms!Form!Path.Caption
DoCmd.TransferSpreadsheet acImport, 10, "EM", Str, True, ""
Import_Exit:
Exit Function
Import_Err:
MsgBox Error$
Resume Import_Exit
End Function
Error: "Object doesn't support this property or method".
As I plan to give the file to others to run, they will have a different path, so we need to prefix it in their system. So instead if we have form path, then users can input the path in form and run it.
Use this:
Str = Forms!Form.Path
I would (in your shoes) use the File Open Dialog instead letting users type the file with path:
Set FD = Application.FileDialog(msoFileDialogOpen)
Dim FileChosen As Integer
FileChosen = FD.show
FD.Title = "Choose workbook"
FD.InitialView = msoFileDialogViewList
FD.Filters.Clear
FD.Filters.add "Excel workbooks", "*.xlsx"
FD.Filters.add "All files", "*.*"
FD.FilterIndex = 1
FD.ButtonName = "Choose this file"
If FileChosen <> -1 Then
'didn't choose anything (clicked on CANCEL)
MsgBox "No file opened", vbCritical
GoTo Exit_Function
Else
fileNamePath = FD.SelectedItems(1)
End If
Set FD = Nothing
and after that
DoCmd.TransferSpreadsheet acImport, 10, "EM", fileNamePath , True
The Access Textbox control doesn't have a Caption property. But it does have two other properties:
Text property
Value property.
AFAICT, Text returns the text currently in the textbox, while Value returns the last persisted value. If the user has modified the text in the textbox but the focus is still in the texbox, Text will return the user-entered new value, but Value will return the value before the user started editing.
Note that you can only get and set Text when the control has the focus.

Saving to PDF adding blank page

I'm trying to create a macro that will allow my end users to click on one macro that will ask them for a PDF file that they would like to add as a new page. The page is based off a template and have a merged cell in the center for the image. I'm able to get the new page created as well as get a file explorer window with specific filters opened, but I have a couple questions for this.
Is it possible to get the full filepath from the file explorer to be put into a variable to later use during the PDF insertion(as my end users may have PDF's saved in different directories)?
Code for the question:
With fd
.AllowMultiSelect = False
' Set the title of the dialog box.
.Title = "Please select the file."
' Clear out the current filters
.Filters.Clear
.Filters.Add "PDF Files", "*.PDF"
.Filters.Add "All Files", "*.*"
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
txtFileName = .SelectedItems(1) 'txtFileName will return the file
End If
End With
Is it possible to insert a PDF image into an excel cell and have it fit the cells dimensions?
Code for the question:
tempRow = lastRow + 9
'Sets the correct row/column
Set rng = crrntWorkbook.Sheets(1).Range("B" & tempRow)
'This portion is trying to insert the PDF as an OLEObject picture
crrntWorkbook.Sheets(1).Range("B" & tempRow) = crrntWorkbook.Sheets(1).OLEObjects.Add(Filename:=txtFileName,_
DisplayAsIcon:=False).activate
Any help with these issues would be greatly appreciated!

How to set FileDialog to not allow double-click

I want to use a file open dialog to extract a file pathway (or open the file if easier)
Is it possible to set the dialog so that it will not open a file if a file-name is double-clicked?
What I want to avoid is if the user double-clicks a file name but that file is already open then a further alert appears.
Or, alternatively, it would work if I set things up so that a read-only version of the file is opened when the user clicks the dialog's OPEN button or double-clicks a file name - is this an easier approach? In this case do I use the dialog's Execute method ?
Private Function FindFilePath() As Boolean
Dim selectedMultiFiles As Boolean
Dim fd As FileDialog
Dim objfl As Variant
Set fd = Excel.Application.FileDialog(msoFileDialogOpen)
Dim myTxt As String
With fd
.Filters.Add "Excel Files", "*.xlsx;*.xlsm", 1
.AllowMultiSelect = False
.Title = "Choose the file with the target table"
.InitialView = msoFileDialogViewDetails
If .Show = -1 Then
myTxt = .SelectedItems.Item(1)
fFileName = myTxt
FindFilePath = True
Else
myTxt = "Nothing was selected"
FindFilePath = False
End If
On Error Resume Next End With
txBoxFilePath.Text = myTxt
End Function
I am not sure how much this would mess your current project up but are you aware of
Dim getPath As Variant
getPath = Application.GetOpenFilename
Debug.Print getPath
where getPath will literally store the path to whatever file the user chose.
It will not open the file automatically unless you actually Set getPath = App..
You can open the file later in your code performing checks for the file being already open or just opening it read-only like you mentioned.

VBA Excel File Open Prompt Cancel Error

So I am using a file open prompt to gather a filename.
I then open this file in a background instance, parse information to a dictionary and close the file.
This works fine.
The code for this is:
Application.FileDialog(msoFileDialogOpen).Show
sFullName = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
If the user presses cancel, which is obviously a feasible scenario, I get the following error:
Invalid procedure call or argument
I have tried to change the 'gather' line to:
If Application.FileDialog(msoFileDialogOpen).SelectedItems(1) Then sFullName = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
However this still brings up the error. Even disabling alerts brings up an error or '400'.
Any help on how to make this popup or handle it would be greatly appreciated.
You need check the bounds to determine if anything was selected
with Application.FileDialog(msoFileDialogOpen)
.Show
if (.SelectedItems.Count = 0) Then
'// dialog dismissed with no selection
else
sFullName = .SelectedItems(1)
end if
end with
You could use GetSaveAsFilenamei.e.
Dim strFileName As String
strFileName = Application.GetSaveAsFilename
If strFileName = "False" Then MsgBox "User cancelled"

Resources