Start New Discussion within our Software Development Community

-->>Hope all is well,I've Managed to write code to Open Folders/Browse Folders using Common Dialog...
-->>The name of Common Dialog is Folder...
-->>But now as I've the Open Button when the Browse Windows Show up,I was wondering how can I...
-->>Open the Selected File when User Click the Open Button and the File to open using its Default -->>application...
-->>My Code on the Module is this:

Public Sub User_Folder(Users As CommonDialog)
'******************************************************************'
'*       COMMON PROCEDURE TO ACCESS USER FOLDER IN A SYSTEM       *'
'******************************************************************'


With Users
        .DialogTitle = "TITLE OF MY BROWSE WINDOW:"
        .InitDir = GetAppPath() & "MY INITIAL FORLDER:"
        .Filter = "(*.bmp; *.jpg; *. gif; *.pcx; *.mp3;*.pcx;)| *.bmp; *.jpg; *.gif; *.pcx; *.mp3;*.pcx;|" & _
                  "(*.wma; *.wav; *.mpg; *.avi; *.VOB;*.png;)|*.wma; *.wav; *.mpg; *.avi; *.VOB;*.png;|" & _
                  "(*.mdb; *.txt; *.doc; *.xlsx; *.pptx;*.psd;)|*.mdb; *.txt; *.doc; *.xlsx; *.pptx;*.psd;|" & _
                  "(*.pub; *.pdf; *.chm; *.docx; *.php;)|*.pub; *.pdf; *.chm; *.docx; *.php;|" & _
                  "(*.html; *.bmp; *.jpg; *.gif; *.dat;)|*.html; *.bmp; *.jpg; *.gif; *.dat;|" & _
                  "(*.All files)|*.*|"
        .ShowOpen
End With

End Sub

-->>On the Form's Command Button I call the Procedure as this:

Private Sub Browse_Folder_Click()
User_Folder Folder
End Sub

-->>Any Suggestions? Thanks...

Not sure what you need Bile...

After the user found the file (via your code above), you want to open say a word document, picture etc? Seems that you will be making use of the shell command then.

-->>Thats Exactly what I mean Andre,but as You can See that I alredy have a Button...
-->>named Open on the browsing window,thus after the User has found and selected...
-->>a file its name shows up in a File name Field of the Browse window what remains is...
-->>just to open it by its default Application when the Button is pressed...
-->>Now how will I use the Shell Command to pic up the default Application for a File?..
-->>For Example I wanted to Open Calculator and I used the following code:

Public WshShell As Object

Dim Calculator As Double
Calculator = Shell("C:\WINDOWS\System32\calc.exe", vbNormalFocus)

-->>It worked fine and I can open the Calculator,so on the case above does it mean that...
-->>First I have to Check for file extension so that I can eccess the required...
-->>apllication to open the file or? Becouse as I can see it looks like I'll have to do...
-->>lots of branching based on My Filtering criteria...
-->>I hope now I've tried to explain Myself?...

Attached is an old sample with probably all shells you will need...

Seeing that my uploads is bombing out, the code. Just add controls accordingly...

Option Explicit
Private Sub Command1_Click()

Dim x As Double
x = Shell("rundll32.exe shell32.dll,Control_RunDLL", vbNormalFocus)
End Sub

Private Sub Command2_Click()
Dim x As Double
x = Shell("rundll32.exe shell32.dll,Control_RunDLL mlcfg32.cpl", vbNormalFocus)
End Sub

Private Sub Command3_Click()
Dim x As Double
x = Shell("rundll32.exe shell32.dll,Control_RunDLL modem.cpl", vbNormalFocus)
End Sub

Private Sub Command4_Click()
Dim x As Double
x = Shell("rundll32.exe shell32.dll,Control_RunDLL netcpl.cpl", vbNormalFocus)
End Sub

Private Sub Option1_Click(Index As Integer)

Dim x As Double
Select Case Index

Case 0
x = Shell("rundll32.exe shell32.dll,Control_RunDLL access.cpl,,1", vbNormalFocus)
Case 1
x = Shell("rundll32.exe shell32.dll,Control_RunDLL access.cpl,,2", vbNormalFocus)
Case 2
x = Shell("rundll32.exe shell32.dll,Control_RunDLL access.cpl,,3", vbNormalFocus)
Case 3
x = Shell("rundll32.exe shell32.dll,Control_RunDLL access.cpl,,4", vbNormalFocus)
Case 4
x = Shell("rundll32.exe shell32.dll,Control_RunDLL access.cpl,,5", vbNormalFocus)
End Select
End Sub

Private Sub Option2_Click(Index As Integer)

Dim x As Double
Select Case Index
Case 0
x = Shell("rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,1", vbNormalFocus)
Case 1
x = Shell("rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,2", vbNormalFocus)
Case 2
x = Shell("rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,3", vbNormalFocus)
End Select
End Sub

Private Sub Option3_Click(Index As Integer)

Dim x As Double
Select Case Index

Case 0

x = Shell("rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,0", vbNormalFocus)
Case 1
x = Shell("rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,1", vbNormalFocus)
Case 2
x = Shell("rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,2", vbNormalFocus)
Case 3
x = Shell("rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,3", vbNormalFocus)
End Select
End Sub

Private Sub Option4_Click(Index As Integer)

Dim x As Double
Select Case Index

Case 0
x = Shell("rundll32.exe shell32.dll,Control_RunDLL intl.cpl,,0", vbNormalFocus)
Case 1
x = Shell("rundll32.exe shell32.dll,Control_RunDLL intl.cpl,,1", vbNormalFocus)
Case 2
x = Shell("rundll32.exe shell32.dll,Control_RunDLL intl.cpl,,2", vbNormalFocus)
Case 3
x = Shell("rundll32.exe shell32.dll,Control_RunDLL intl.cpl,,3", vbNormalFocus)
Case 4
x = Shell("rundll32.exe shell32.dll,Control_RunDLL intl.cpl,,4", vbNormalFocus)
End Select
End Sub

Private Sub Option5_Click(Index As Integer)

Dim x As Double
Select Case Index

Case 0

x = Shell("rundll32.exe shell32.dll,Control_RunDLL main.cpl @0", vbNormalFocus)
Case 1
x = Shell("rundll32.exe shell32.dll,Control_RunDLL main.cpl @1", vbNormalFocus)
Case 2
x = Shell("rundll32.exe shell32.dll,Control_RunDLL main.cpl @2", vbNormalFocus)
Case 3
x = Shell("rundll32.exe shell32.dll,Control_RunDLL main.cpl @3", vbNormalFocus)
End Select
End Sub

Private Sub Option6_Click(Index As Integer)

Dim x As Double
Select Case Index

Case 0
x = Shell("rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,0", vbNormalFocus)
Case 1
x = Shell("rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,1", vbNormalFocus)
'Case 2
'x = Shell("r rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,2", vbNormalFocus)
'Case 3
'x = Shell(" rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,3", vbNormalFocus)
Case 4
x = Shell("rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,4", vbNormalFocus)
Case 5
x = Shell("rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl @1", vbNormalFocus)
End Select
End Sub

Private Sub Option7_Click(Index As Integer)
Dim x As Double
Select Case Index

Case 0
x = Shell("rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,0", vbNormalFocus)
Case 1
x = Shell("rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,1", vbNormalFocus)
Case 2
x = Shell("rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,2", vbNormalFocus)
Case 3
x = Shell("rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,3", vbNormalFocus)
Case 4
x = Shell("rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl @1", vbNormalFocus)
End Select
End Sub
Comments
glad to know it's not just me having problems uploading. Lately it takea 2 tries to get something thru

Here is another way.

Private Declare Function AssocQueryString Lib "shlwapi.dll" Alias "AssocQueryStringA" _
    (ByVal flags As AssocF, _
     ByVal str As AssocStr, _
     ByVal pszAssoc As String, _
     ByVal pszExtra As String, _
     ByVal pszOut As String, _
     ByRef pcchOut As Long) As Long

Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
    ByVal hWnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Integer) As Long

Enum AssocF
    Init_NoRemapCLSID = &H1
    Init_ByExeName = &H2
    Open_ByExeName = &H2
    Init_DefaultToStar = &H4
    Init_DefaultToFolder = &H8
    NoUserSettings = &H10
    notruncate = &H20
    Verify = &H40
    RemapRunDll = &H80
    NoFixUps = &H100
    IgnoreBaseClass = &H200
End Enum

Enum AssocStr
    Command = 1
    Executable
    FriendlyDocName
    FriendlyAppName
    NoOpen
    ShellNewValue
    DDECommand
    DDEIfExec
    DDEApplication
    DDETopic
    INFOTIP
    QUICKTIP
    TILEINFO
    CONTENTTYPE
    DEFAULTICON
    SHELLEXTENSION
    DROPTARGET
    DELEGATEEXECUTE
    SUPPORTED_URI_PROTOCOLS
    Max
End Enum

Private Const S_OK As Long = &H0
Private Const MAX_PATH As Long = 260
  Public Function GetAsscociatedExe(ByVal doctype As String) As String
    ' Allocate the output buffer

    GetAsscociatedExe = String$(MAX_PATH, " ")
    Dim pcchOut As Long  ' size of output buffer
    pcchOut = MAX_PATH

    ' Get the full pathname to the program in pszOut
    Dim result As Long
    result = AssocQueryString(AssocF.Verify, _
                              AssocStr.Executable, _
                              doctype, _
                              "open", _
                              GetAsscociatedExe, _
                              pcchOut)


    If result <> 0 Then
        GetAsscociatedExe = vbNullString
    Else
        GetAsscociatedExe = Left$(GetAsscociatedExe, pcchOut - 1)
    End If

  End Function


Sub test()

    ' method 1: get associate file
    Dim exten As String
    exten = ".docx"

    Dim exe As String
    exe = GetAsscociatedExe(exten)

    If exe <> vbNullString Then

        Dim res1 As Double
        ' Note the quotes around the filename and the space before the filename
        res1 = Shell(exe & " ""D:\My Documents\TestWordFile.docx""", vbNormalFocus)
    Else
        MsgBox ("No executable associated with " & exten)
    End If

    ' method 2: ShellExecute
    If exe <> vbNullString Then
        Dim res2 As Long
        res2 = ShellExecute(0, _
                            "open", _
                            """D:\My Documents\TestWordFile.docx""", _
                            vbNullString, _
                            vbNullString, _
                            vbNormalFocus)
    Else
        MsgBox ("No executable associated with " & exten)
    End If
End Sub

-->>Wow, I admit that Your code is realy good for Me as also it has add something to Me exept that...
-->>May be I didn't explain My self very much it is not what I meant,May be I should put it this way...
-->>Suppose I have a Form namely "Files" with a Command Button namely "Search"...
-->>add a common dialog namely "Folder" on a Form (Files) and refference it in Our Project...
-->>and a Module namely "Folder Files"...
-->>Now in the Module (Folder Files) lets Paste the Following code:

Public Sub User_Folder(Users As CommonDialog)
'******************************************************************'
'*       COMMON PROCEDURE TO ACCESS USER FOLDER IN A SYSTEM       *'
'******************************************************************'


With Users
        .DialogTitle = "Open Selected File:"
        .InitDir = 'Put any Initial Directory of Your own
        .Filter = "(*.bmp; *.jpg; *. gif; *.pcx; *.mp3;*.pcx;)| *.bmp; *.jpg; *.gif; *.pcx; *.mp3;*.pcx;|" & _
                  "(*.wma; *.wav; *.mpg; *.avi; *.VOB;*.png;)|*.wma; *.wav; *.mpg; *.avi; *.VOB;*.png;|" & _
                  "(*.mdb; *.txt; *.doc; *.xlsx; *.pptx;*.psd;)|*.mdb; *.txt; *.doc; *.xlsx; *.pptx;*.psd;|" & _
                  "(*.pub; *.pdf; *.chm; *.docx; *.php;)|*.pub; *.pdf; *.chm; *.docx; *.php;|" & _
                  "(*.html; *.bmp; *.jpg; *.gif; *.dat;)|*.html; *.bmp; *.jpg; *.gif; *.dat;|" & _
                  "(*.All files)|*.*|"
        .ShowOpen
End With

End Sub

-->>And on the Command Button (Search) Click event lets Paste the Following code:

Private Sub Search_Click()
User_Folder Folder
End Sub

-->>Now when we Click the Command Button (Search) an Explorer Windows Open with the Specified...
-->>Initial Directory,form the Directory Pic any File that recides there Say an .Mp3 file or ...
-->>Word File .docx or what ever the File You have then Its name will Appear in a Text or ...
-->>Some thing Like Combobox on the Window then there You have also two Buttons Open and Cancel...
-->>where with Open You have two Choices 1.Open and 2.Open as read-only/for write...
-->>Now I want when I click the Open Button the Selected File to Open By its Default Application...
-->>How is that going to be accomplished?
-->>But once again AndreRet and TnTinMN,I appreciate Your help also helped me...
-->>@ AndreRet the Code TnTinMn Provided worked fine for me and Managed to Open word file that I ...
-->>Located on line 95 and 105,thanks...

-->>Nop,thats NOT exactly what I mean I can use the CommonDialog to retrieve the selected file just fine
-->>and I hope that if You can follow the code above can assist Me to do so just by adding Filename...
-->>Or File Title than get it in what ever the control I have prepared for suc as Text box.
-->>But I mean after I have the File Selected from the Dialog that appears,may be I should say ...
-->>"HOW CAN I RUN IT BY ITS DEFAULT SOFTWARE BASED ON ITS TYPE AND EXTENSION"?...
-->>Say its an MP3 Audio Music File to be Rub may be by VLC or WMPlayer once the I press the Open...
-->>Button on the Dialog (The File Should Automatic Pic its default Software and Run) so that I can...
-->>listen to a Selected Music...
-->>Or is it NECESSARY that I get its name or title first in My application and have another...
-->>Button to Run it?

If I read this correctly, you want to read what type of file extension the selection have and then open that file with its own application, i.e. .doc = ms word, .xls = ms excel etc...

Get the users selection into a variable say strFile, use InStr to read the parts after the . (dot) and then use shell to open accordingly...

-->>I hope You all drove Me to where I wanted Guys,thats sure AndereRet for what You understood...
-->>@ TnTinMN : This statement
"Are you trying to tell us that you do not understand how to use the CommonDialog to retrieve the selected file?"
-->>Helped Me to Open My Eyes a bit wider,I knew that there is something in Your Code and so I took ...
-->>a time to Study it then Modify it and hence got Your Point!!!...
-->>I was a little bit blind to see but all I needed was in Your Code let Me show some modification...
-->>thought not exactly total modification but based on My demands here it goes:
-->>I have a Form (Files) with a Command Button (Browse) and a CommonDialog (Folder):..
-->>I have a Module with TnTinMN's Code a little bit modified to this in the Module:

Option Explicit
Private Declare Function AssocQueryString Lib "shlwapi.dll" Alias "AssocQueryStringA" _
    (ByVal flags As AssocF, _
    ByVal str As AssocStr, _
    ByVal pszAssoc As String, _
    ByVal pszExtra As String, _
    ByVal pszOut As String, _
    ByRef pcchOut As Long) As Long
    Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
    ByVal hWnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Integer) As Long
    Enum AssocF
    Init_NoRemapCLSID = &H1
    Init_ByExeName = &H2
    Open_ByExeName = &H2
    Init_DefaultToStar = &H4
    Init_DefaultToFolder = &H8
    NoUserSettings = &H10
    notruncate = &H20
    Verify = &H40
    RemapRunDll = &H80
    NoFixUps = &H100
    IgnoreBaseClass = &H200
    End Enum
    Enum AssocStr
    Command = 1
    Executable
    FriendlyDocName
    FriendlyAppName
    NoOpen
    ShellNewValue
    DDECommand
    DDEIfExec
    DDEApplication
    DDETopic
    INFOTIP
    QUICKTIP
    TILEINFO
    CONTENTTYPE
    DEFAULTICON
    SHELLEXTENSION
    DROPTARGET
    DELEGATEEXECUTE
    SUPPORTED_URI_PROTOCOLS
    Max
    End Enum
    Private Const S_OK As Long = &H0
    Private Const MAX_PATH As Long = 260
    Public Function GetAsscociatedExe(ByVal Docment_Type As String) As String

    GetAsscociatedExe = String$(MAX_PATH, " ")

    Dim pcchOut As Long
    pcchOut = MAX_PATH

    Dim Result As Long

    Result = AssocQueryString(AssocF.Verify, AssocStr.Executable, Docment_Type, "open", GetAsscociatedExe, pcchOut)
    If Result <> 0 Then
    GetAsscociatedExe = vbNullString
    Else
    GetAsscociatedExe = Left$(GetAsscociatedExe, pcchOut - 1)
    End If
    End Function

    Public Sub Open_File(File_Name As String)

     Dim File_Extension As String
     Dim Executable_Program As String
     Dim Start_Program As Long

     File_Extension = ".MP3"
     Executable_Program = GetAsscociatedExe(File_Extension) 

    '' I TOOK METHOD 2 : ShellExecute
     If Executable_Program <> vbNullString Then

     Start_Program = ShellExecute(0, "open", "" & File_Name & "", vbNullString, vbNullString, vbNormalFocus)
     Else
     MsgBox "Sorry  Can't Find a Program to Open " & File_Extension & " File Extension.", vbOKOnly + vbInformation, "You need a Program to Open Your File"
     End If
    End Sub

-->>And this for CommonDialog to Showup and some filtering criteria and so forth...

Public Sub User_Folder(Users As CommonDialog)
'******************************************************************'
'*       COMMON PROCEDURE TO ACCESS USER FOLDER IN A SYSTEM       *'
'******************************************************************'


With Users
            '**************************************'
            '* SETS THE DIALOG TITLE TO OPEN FILE *'
            '**************************************'
        .DialogTitle = USER_NAME & "'S SPGPAC - File DOCUMENTS."
            '***************************************'
            '* SETS THE DIALOG'S INITIAL DIRECTORY *'
            '***************************************'
        .InitDir = GetAppPath() & "SPGPAC - File User Files\" & USER_NAME & "\"
            '**************************'
            '* SETS THE FILE LIST BOX *'
            '**************************'
        .Filter = "(*.bmp; *.jpg; *. gif; *.pcx; *.mp3;*.pcx;)| *.bmp; *.jpg; *.gif; *.pcx; *.mp3;*.pcx;|" & _
                  "(*.wma; *.wav; *.mpg; *.avi; *.VOB;*.png;)|*.wma; *.wav; *.mpg; *.avi; *.VOB;*.png;|" & _
                  "(*.mdb; *.txt; *.doc; *.xlsx; *.pptx;*.psd;)|*.mdb; *.txt; *.doc; *.xlsx; *.pptx;*.psd;|" & _
                  "(*.pub; *.pdf; *.chm; *.docx; *.php;)|*.pub; *.pdf; *.chm; *.docx; *.php;|" & _
                  "(*.html; *.bmp; *.jpg; *.gif; *.dat;)|*.html; *.bmp; *.jpg; *.gif; *.dat;|" & _
                  "(*.All files)|*.*|"
            '******************************************'
            '* SET THE DEFAULT FILES TYPE TO ALL FIES *'
            '******************************************'
        .FilterIndex = 28
            '*******************************************************'
            '* SETS THE FLAGS - FILE MUST EXIST AND HIDE READ ONLY *'
            '*******************************************************'
        .flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
            '*******************************************************************'
            '* SET DIALOG BOX SO AN ERROR OCCURS IF THE DIALOGBOX IS CANCELLED *'
            '*******************************************************************'
        .CancelError = True
            '************************************************'
            '* ENABLES ERROR HANDLING TO CATCH CANCEL ERROR *'
            '************************************************'
            On Error Resume Next
        '**************************'
        '* DISPLAY THE DIALOG BOX *'
        '**************************'
        .ShowOpen

        If Err Then
            '**********************************************'
            '* THIS CODE RUNS IF THE DIALOG WAS CANCELLED *'
            '**********************************************'
            MsgBox "Browsing Dialog Cancelled", vbOKOnly + vbInformation, "User Documents Cancelled"
            Exit Sub
        End If

'***************************'
'* DISPLAYS A MESSAGE BOX. *'
'***************************'
Open_File .FileTitle

End With

End Sub

-->>NOTE LINE 57: Calls the Function Open_File() in this Module and Pass the File Name of the...
-->>CommonDialog Users and so execution goes there...
-->>Now back to the form Files in Command Button Browse I have this code:

User_Folder Folder

-->>Only Calling the Sub User_Folder and everything was NICE and as I wanted...
-->>But @ TnTinMN why Should I initialize File_Extension as I did or what You did in Your code...
-->>exten = ".docx" though I used File_Extension = ".MP3" to only One Type of file Extension...
-->>while I have Lots of files to be Open as Per Filtering Criteria?...
-->>By omiting the Line an Error Occurs so is there a way I can Just state that all type of file...
-->>Extension is equal ti exten or File_Extension (in My Line)?
-->>Although all files get opened appart of what Extension has been Initialized...
-->>@ AndreRet I hope as well You will now be able to Open Your Word Files with TnTinMN Code...
-->>Thaks Once again AndreRet and TnTinMN...

Comments
Nice for taking advice and make it work. Not just copy and paste. thanx :)
This question has already been answered. Start a new discussion instead.