Open file location (1 Viewer)

mester

Member
Local time
Today, 16:46
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?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:46
Joined
May 7, 2009
Messages
19,175
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
 

mester

Member
Local time
Today, 16:46
Joined
Apr 2, 2020
Messages
63
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
 

mester

Member
Local time
Today, 16:46
Joined
Apr 2, 2020
Messages
63
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
 

June7

AWF VIP
Local time
Today, 07:46
Joined
Mar 9, 2014
Messages
5,425
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
 

mester

Member
Local time
Today, 16:46
Joined
Apr 2, 2020
Messages
63
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.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:46
Joined
Oct 29, 2018
Messages
21,360
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...
 

mester

Member
Local time
Today, 16:46
Joined
Apr 2, 2020
Messages
63
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
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:46
Joined
Sep 21, 2011
Messages
14,058
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?
 

mester

Member
Local time
Today, 16:46
Joined
Apr 2, 2020
Messages
63
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.
 

mester

Member
Local time
Today, 16:46
Joined
Apr 2, 2020
Messages
63
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

Top Bottom