Office.FileDialog not performing as I expected - excel

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

Related

Default file selection using Application.GetOpenFilename

I use the following to open a dialog box to select files.
fnameListCurr = Application.GetOpenFilename(FileFilter:="CSV Files (*.csv),*.csv", Title:="Choose CSV files to import", MultiSelect:=True)
However, when I do this, nothing is selected by default. Is it possible to have the file selector dialog open with certain files or all files that meet the filter criteria already selected?
If you need to set an initial Fine name, use FileDialog instead. Try the next code, please:
Sub filedialogSelectSomething()
Dim fileName
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = True
.Title = "Choose CSV files to import"
.InitialFileName = "Your Path\Test*.csv"
.Filters.Add "CSV Files", "*.csv", 1
If .Show = -1 Then
fileName = .SelectedItems(1)
'or use them all...
End If
End With
Debug.Print fileName
End Sub
Look here to better understand how InitialFileName overrides the file filter settings...

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!

InitialFileName of FileDialog doesn't display whole file name

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.

VBA 2007 Filedialog Adding Filters

I'm using Access 2007 and trying to filter the FileDialog list of files by using a Filter. While debugging, the code returns an error on the .Filters.Add line: Run-time error '5': Invalid procedure call or argument. I've scoured StackOverflow and Microsoft. The .Add method appears to be correct.
Dim fn As Object
Set fn = Application.FileDialog(3)
With fn
.InitialFileName = "C:\BillData\"
.Title = "Select input file"
.Filters.Clear
.Filters.Add "Bill Files", "ccc.*"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewDetails
End With
If I skip that line of code, everything else seems to work properly. Your help would be greatly appreciated.
Assuming you are looking for files like "test.ccc" or "myresource.ccc", your filter should be "*.ccc". If you actually have a bunch of files named just ccc of different file types like "ccc.txt", "ccc.jpg" "ccc.doc" etc, you would need to filter for those through InitialFileName like this.

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.

Resources