Opening a folder using windows explorer from a partitioned drive

Kemidan2014

Board Regular
Joined
Apr 4, 2022
Messages
226
Office Version
  1. 365
Platform
  1. Windows
I decided to change an onclick event to behave from opening an excel tile to opening the directory that file would be in so that all the files that would have been placed there are readily accessible.

Google research offered up some coding using Shell method. but when I run it i get Run time error 53 file not found but the directory DOES exist. So i dont know if this is an issue where i am searching for a file thats located on a network drive or not.


Here is the code

VBA Code:
Private Sub OpenA3_Click()
Dim foldername As String
Dim sfilename As String
'Dim sfolderexists As String
'Dim excelapp As Object
'Set excelapp = CreateObject("Excel.Application")
 
foldername = Me.[QIMS#]

 'sfilename = "O:\1_All Customers\Current Complaints\Complaint Folders" & "\" & foldername & "\" & foldername & ".xls"
 sfilename = "O:\1_All Customers\Current Complaints\Complaint Folders" & "\" & foldername & "\."
 If Dir(sfilename, vbDirectory) = "" Then
 MsgBox ("Complaint folder Does not exist!")
 'MsgBox ("A3 Does not exist!")
 Exit Sub
 End If
 
 Shell "C:\Windows\explorer.exe """ & sfilename & "", vbNormalFocus
 
'excelapp.Workbooks.Open (sfilename)
'excelapp.Visible = True
'Set excelapp = Nothing

End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
paste this code into a module, then usage:
OpenNativeApp txtBox


this will open ANY file in its native app
.Docx will open in word,
.xlsx opens in Excel

a file path will open in explorer.


Code:
Option Compare Database
Option Explicit

#If Win64 Then
  'Declare PtrSafe Sub...
    Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
    Declare PtrSafe Function GetDesktopWindow Lib "user32" () As Long
#Else
   Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
   Private Declare Function GetDesktopWindow Lib "user32" () As Long
#End If

Const SW_SHOWNORMAL = 1
Const SE_ERR_FNF = 2&
Const SE_ERR_PNF = 3&
Const SE_ERR_ACCESSDENIED = 5&
Const SE_ERR_OOM = 8&
Const SE_ERR_DLLNOTFOUND = 32&
Const SE_ERR_SHARE = 26&
Const SE_ERR_ASSOCINCOMPLETE = 27&
Const SE_ERR_DDETIMEOUT = 28&
Const SE_ERR_DDEFAIL = 29&
Const SE_ERR_DDEBUSY = 30&
Const SE_ERR_NOASSOC = 31&
Const ERROR_BAD_FORMAT = 11&

Public Sub OpenNativeApp(ByVal psDocName As String)
Dim r As Long, Msg As String

r = StartDoc(psDocName)
If r <= 32 Then
    'There was an error
    Select Case r
        Case SE_ERR_FNF
            Msg = "File not found"
        Case SE_ERR_PNF
            Msg = "Path not found"
        Case SE_ERR_ACCESSDENIED
            Msg = "Access denied"
        Case SE_ERR_OOM
            Msg = "Out of memory"
        Case SE_ERR_DLLNOTFOUND
            Msg = "DLL not found"
        Case SE_ERR_SHARE
            Msg = "A sharing violation occurred"
        Case SE_ERR_ASSOCINCOMPLETE
            Msg = "Incomplete or invalid file association"
        Case SE_ERR_DDETIMEOUT
            Msg = "DDE Time out"
        Case SE_ERR_DDEFAIL
            Msg = "DDE transaction failed"
        Case SE_ERR_DDEBUSY
            Msg = "DDE busy"
        Case SE_ERR_NOASSOC
            Msg = "No association for file extension"
        Case ERROR_BAD_FORMAT
            Msg = "Invalid EXE file or error in EXE image"
        Case Else
            Msg = "Unknown error"
    End Select
'    MsgBox msg
End If
End Sub

Private Function StartDoc(psDocName As String) As Long
Dim Scr_hDC As Long

Scr_hDC = GetDesktopWindow()
StartDoc = ShellExecute(Scr_hDC, "Open", psDocName, "", "C:\", SW_SHOWNORMAL)
End Function
 
Upvote 0
Works for me?, so walk your code

Code:
Private Sub OpenA3_Click()
Dim foldername As String
Dim sfilename As String
'Dim sfolderexists As String
'Dim excelapp As Object
'Set excelapp = CreateObject("Excel.Application")
 
foldername = "YouTube"

 'sfilename = "O:\1_All Customers\Current Complaints\Complaint Folders" & "\" & foldername & "\" & foldername & ".xls"
 sfilename = "Z:\Downloads\YouTube\" & foldername & "\"
 If Dir(sfilename, vbDirectory) = "" Then
 MsgBox ("Complaint folder Does not exist!")
 'MsgBox ("A3 Does not exist!")
 Exit Sub
 End If
 
 Shell "C:\Windows\explorer.exe """ & sfilename & "", vbNormalFocus
 
'excelapp.Workbooks.Open (sfilename)
'excelapp.Visible = True
'Set excelapp = Nothing

End Sub
 
Upvote 0
Okay, that is odd after i closed and restarted the app (cause i was called out to the floor) it did infact work? Apologies for the posting. I do not know why closing and re-opening access would allow it to work. ive changed nothing to it since i made the post.
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,988
Members
449,093
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top