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.
Related
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
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...
This question already has an answer here:
Return VBA Network Drive Letter for Different Users
(1 answer)
Closed 3 years ago.
For the project I am doing, I am going to open up a csv file based on user's choice. The csv file would sit in folder like \\server\share\folder\file.csv or M:\folder\file.csv.
dim PATH As String: PATH = "M:\folder\file.csv"
ChDrive (Left(PATH, 1)) ' point to the drive
ChDir PATH
' get csv file name
csv_file_name = Application.GetOpenFilename("CSV Files (*.csv), *.csv", , "Select a CSV file", , False)
The code above opens up a select file box at PATH.
The problem is, not all the machines have the same network drive mapping. For the same \\server\share combo, some people might map it to M and others might map it to Z. So instead, I was wondering if I can do something like ChDrive "\\server\share\". Unfortunately, ChDrive only takes drive letter as paramter.
I was wondering if there's a more generic solution to a problem like this. Thanks!
You can write a function using the msoFileDialogOpen option and specify an initial filename:
Function FileOpen(initialFilename As String, Optional sDesc As String = "CSV Files (*.csv)", Optional sFilter As String = "*.csv") As String
With Application.FileDialog(msoFileDialogOpen)
.initialFilename = initialFilename
.Filters.Clear
.Filters.Add sDesc, sFilter, 1
.Title = "File Open"
.AllowMultiSelect = False
If .Show = -1 Then FileOpen = .SelectedItems(1)
End With
End Function
Usage:
Sub Test()
MsgBox FileOpen("\\server\share", "Select a File", "*.csv")
End Sub
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!
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.