How to set FileDialog to not allow double-click - excel

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.

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

create multiple sheets as pdf

I have this section of code that asks for a folder to save PDFs to. If you cancel or close the application, the path of the file is still selected. I want to select the file from the path only if you don't select a folder, and I don't want any action to be taken if you press cancel or close the application.
Dim folder_path as string
Dim sh as worksheet
Dim filename as string
with application.filedialog(msofiledialogfolderpicker)
.title = "Select the folder path"
if .show = -1 then
Folder_path = .selectedItems(1)
else
Folder_path = Thisworkbook.path
end if
end with```

Access 2010: Import Excel File which is selected with a FileDialog

Im new to Access and VBA.
I created a form in which i can select a file via fileDialog.
Here the code for the fileDialog:
Public Function DateiAuswaehlen()
Dim objFiledialog As FileDialog
Set objFiledialog = _
Application.FileDialog(msoFileDialogOpen)
With objFiledialog
.AllowMultiSelect = False
If .Show = True Then
DateiAuswaehlen = .SelectedItems(1)
End If
End With
Set objFiledialog = Nothing
End Function
How can I import the selected Excel File into an access table?
I found the DoCmd.TransferSpreadsheet Method but it has not worked, tbh i dont even know where to place it. Im sorry as I mentioned im very new to VBA
Here you go!!
Sub btn_GetFileName_Click()
'************************************************************************
'Lets get the file name
Debug.Print "Getting File Name"
'Declare a variable as a FileDialog object.
Dim fd As FileDialog
'Set the starting look location
Dim strComPath As String
strComPath = "C:\"
Dim strFilePath As String
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Declare a variable to contain the path
'of each selected item. Even though the path is a String,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant
'Use a With...End With block to reference the FileDialog object.
With fd
.InitialFileName = strComPath
.AllowMultiSelect = False
.Filters.Clear
'Add filter to only show excel files.
.Filters.Add "Excel files", "*.xlsm", 1
'Use the Show method to display the File Picker dialog box and return the user's action.
'The user pressed the action button.
If .Show = -1 Then
strFilePath = .SelectedItems(1)
'Step through each string in the FileDialogSelectedItems collection.
'For Each vrtSelectedItem In .SelectedItems
'vrtSelectedItem is a String that contains the path of each selected item.
'You can use any file I/O functions that you want to work with this path.
'This example simply displays the path in a message box.
' strFilePath: " & vrtSelectedItem
'Next vrtSelectedItem
Else
'The user pressed Cancel.
DoCmd.Hourglass (False)
MsgBox "You must select a file to import before proceeding", vbOKOnly + vbExclamation, "No file Selected, exiting"
Set fd = Nothing
Exit Sub
End If
End With
tblFileName = strFilePath
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblTest", tblFileName, True
Set fd = Nothing
End Sub

Excel, VBA, Macro, File - Open or Create, Dialog

I am writing a macro in MS excel using VBA. I need to open or create a file to write to.
Potentially the file may have a different extension (i.e. .cal) but internally it just contains text.
I have looked over a lot of examples that create a file by explicitly stating the path for the new file (here's one I found):
strFileName = "C:\test.txt"
Open strFileName For Output As #iFileNumber
Other examples open a file which already exists.
I would like to have a popup/dialog which allows the user to "either" open an existing file "or" create a new one. I assume this is possible.
I have played around with the Application.FileDialog(....) function using strings/paths and objects without much success so far.
With Application.FileDialog(...) your user should be able to create a new text file as they would in Windows Explorer (by right-clicking and selecting New->Text File), they can then select that file to output data to.
The below SelectFile(...) function returns the path to a selected file (or an empty string if no file was selected). Using this function as-is it is only possible for the user to select one file, but given the context I would hope this isn't a problem.
Public Sub SelectOrCreateFile()
Dim strFileName As String
Dim iFileNum As Integer
iFileNum = FreeFile
strFileName = SelectFile
If strFileName <> "" Then
Open strFileName For Output As #iFileNum
'### WRITE YOUR DATA ###
Close #iFileNum
End If
End Sub
'Returns File Path of file selected with file selection dialog
Public Function SelectFile(Optional DefaultPath As String = "", _
Optional FileType As String = "All Files", _
Optional FileExtension As String = "*.*") As String
Dim F As Object
Set F = Application.FileDialog(msoFileDialogFilePicker)
'Set up FileDialog properties
F.Filters.Clear
F.Filters.Add FileType, FileExtension
F.AllowMultiSelect = False
F.Title = "Select File"
F.ButtonName = "Select"
If DefaultPath <> "" Then F.InitialFileName = DefaultPath
'FileDialog.Show returns False if the user cancels
If F.show Then
SelectFile = F.SelectedItems(1)
Else
MsgBox "No File Selected", vbInformation, "Cancelled"
SelectFile = ""
End If
Set F = Nothing
End Function
Hope this helps!

Importing folder to Excel (FileDialogFolderPicker) using VBA

I'm using the next code in order to select a folder from a certain path and import all the files inside it:
Function GetFolder()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
fd.Title = "Select Excel Workbook(s) Folder"
Dim vrtSelectedItem As Variant
With fd
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
GetFolder = vrtSelectedItem
Next vrtSelectedItem
Else
End If
End With
Set fd = Nothing
End Function
When the Folder Picker window opens it start on the desktop. Is there a way to make it go to a specific path upon opening? or open where the excel file itself is located?
You would update the InitialFileName property, and you could set it to use the ActiveWorkbook.Path
You'll need to make sure that you include the ending slash, or it will only display the previous folder instead of the folder you want.
Also, there is no reason to loop through the .SelectedItems collection because the FolderPicker FileDialog doesn't support mutliple selections.
In summary, I think this is the code you're looking for:
Function GetFolder()
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = ActiveWorkbook.Path & Application.PathSeparator
.Title = "Select Excel Workbook(s) Folder"
If .Show = True Then
GetFolder = .SelectedItems(1)
Else
GetFolder = False
End If
End With
End Function
add a line like this before .Show:
fd.InitialFileName = "c:\whateverInitialDirectory"

Resources