Search for file in userform and return directory and open pdf file

pleb103

New Member
Joined
Jan 3, 2019
Messages
1
Hi All,

I have an urgent request.

In a userform using Browsebutton, user will search for partial text in textbox then click browse. I would like this to open directory in " G: 'textbox.text'/ folder ", then user can open pdf/word/excel formatted files, in the applicable/relevant applications.

Many Thanks in advance!
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
paste this code either in the form, or a module.
(if form, youd need to alter the part to get the folder and key word at: ADD YOUR SEARCH PARAMS HERE)
add a button to run: FindPartNameInDir
it posts the results in col.A.


add another button to run OpenPickedFile,
the code will open the 'found' file in its app.




Rich (BB code):
Public Sub OpenPickedFile()
If ActiveCell.Value <> "" Then OpenNativeApp ActiveCell.Value
End Sub


'-------------
Public Sub FindPartNameInDir()
'-------------
Dim FSO, oFolder, oFile, oRX
Dim sTxt As String, sFile As String, sAct As String
Dim vFile, vFList, vSrc, vTarg
Dim vSrcDir, vPart
Dim i As Integer


On Error GoTo err1


'------ ADD YOUR SEARCH PARAMS HERE


vSrcDir = Range("D7").Value
vPart = LCase(Range("D8").Value)
'----------


If Right(vSrcDir, 1) <> "\" Then vSrcDir = vSrcDir & "\"


Columns("A:A").Clear
Range("A1").Value = "Found"
Range("A2").Select


Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = FSO.GetFolder(vSrcDir)  'use given folder


For Each oFile In oFolder.Files
  'If InStr(oFile.Name, ".xls") > 0 Then
       'vFile = pvDir & oFile.Name       'full path
       vFile = LCase(oFile.Name)
            
       If InStr(vFile, vPart) > 0 Then
          ActiveCell.Value = oFile
          ActiveCell.Offset(1, 0).Select 'next cell
       End If


    i = i + 1
Next
MsgBox "Done"


endit:
Set oFile = Nothing
Set oFolder = Nothing
Set FSO = Nothing
Exit Sub


err1:
Resume endit
'Resume skipit
'Resume
End Sub






paste this in its own module to open any file.


Rich (BB code):
Option Explicit


#If  VBA7 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 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
   'Private Declare ptrsafe 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

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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