Question

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
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top