Open file location

mester

Member
Local time
Today, 15:12
Joined
Apr 2, 2020
Messages
63
Hi everyone,
I have a form where i have a text zone and a botton. I need a vba code to use in that botton which help me to only open the file location that i have its path in the text zone.
Let's say the text zone is named "txtfilepath" and the botton is named "openfilelocation".
Could anyone help me to do that please?
 
Code:
Private Sub openfilelocation_Click()
Dim i As Integer
If Len(Me!txtfilepath & "") <> 0 Then
    If Len(Dir$(Me!txtfilepath)) <> 0 Then
        i = InstrRev(Me!txtfilepath, "\")
        If i <> 0 Then
             Application.FollowHyperLink Left$(Me!txtfilepath, i)
        Else
            Application.FollowHyperlink Me!txtfilepath
        End If
    End If
End If
End Sub
 
Code:
Private Sub openfilelocation_Click()
Dim i As Integer
If Len(Me!txtfilepath & "") <> 0 Then
    If Len(Dir$(Me!txtfilepath)) <> 0 Then
        i = InstrRev(Me!txtfilepath, "\")
        If i <> 0 Then
             Application.FollowHyperLink Left$(Me!txtfilepath, i)
        Else
            Application.FollowHyperlink Me!txtfilepath
        End If
    End If
End If
End Sub
Thanks a lot ,i'll try it now
 
Code:
Private Sub openfilelocation_Click()
Dim i As Integer
If Len(Me!txtfilepath & "") <> 0 Then
    If Len(Dir$(Me!txtfilepath)) <> 0 Then
        i = InstrRev(Me!txtfilepath, "\")
        If i <> 0 Then
             Application.FollowHyperLink Left$(Me!txtfilepath, i)
        Else
            Application.FollowHyperlink Me!txtfilepath
        End If
    End If
End If
End Sub
Hi Sir, this code just show me the folder where my file exist, but the folder has many pdf files, so this code doesn't select the file , i want a code to only select the file not open it. Thanks a lot
 
What do you mean by 'only select the file'? You want to open FileDialog with that file highlighted as default in the dialog?

Code:
   Dim fDialog As Office.FileDialog
 
   ' Set up the File Dialog.
   Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

   With fDialog
      ' Allow user to make multiple or only one selection in dialog box
      .AllowMultiSelect = False
      ' Set the title of the dialog box.
      .title = "Please select one file."
      ' Set the default folder/file path
      .InitialFileName = Me.txtFilePath
      ' Clear out the current filters, and add our own.
      .Filters.Clear
      .Filters.Add "PDF file", "*.pdf"
      ' 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
         Debug.Print .SelectedItems(1)
      Else
         MsgBox "You clicked Cancel in the file dialog box."
      End If
   End With
 
What do you mean by 'only select the file'? You want to open FileDialog with that file highlighted as default in the dialog?

Code:
   Dim fDialog As Office.FileDialog
 
   ' Set up the File Dialog.
   Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

   With fDialog
      ' Allow user to make multiple or only one selection in dialog box
      .AllowMultiSelect = False
      ' Set the title of the dialog box.
      .title = "Please select one file."
      ' Set the default folder/file path
      .InitialFileName = Me.txtFilePath
      ' Clear out the current filters, and add our own.
      .Filters.Clear
      .Filters.Add "PDF file", "*.pdf"
      ' 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
         Debug.Print .SelectedItems(1)
      Else
         MsgBox "You clicked Cancel in the file dialog box."
      End If
   End With
Exactly Sir, i want the code to only highlight the file , which help to see it between all the files in that folder.
 
Exactly Sir, i want the code to only highlight the file , which help to see it between all the files in that folder.
You could try using the Shell function to open Windows Explorer with the /select switch. For example:
Code:
Shell "c:\windows\explorer.exe /select " & Me.Fileoath
Hope that helps...
 
You could try using the Shell function to open Windows Explorer with the /select switch. For example:
Code:
Shell "c:\windows\explorer.exe /select " & Me.Fileoath
Hope that helps...
Thanks Sir ,i'll try it and hope it will work
 
Exactly Sir, i want the code to only highlight the file , which help to see it between all the files in that folder.
And you are going to do what with it afterwards?
 
And you are going to do what with it afterwards?
i have an app where i download files in it, so after knowing the file i already download i can then download the next one.
 
You could try using the Shell function to open Windows Explorer with the /select switch. For example:
Code:
Shell "c:\windows\explorer.exe /select " & Me.Fileoath
Hope that helps...
Thank you so much, it works very well for me.
 

Users who are viewing this thread

Back
Top Bottom