Connecting Tech Pros Worldwide Forums | Help | Site Map

Using FileDialog property to open and display FileName in a textbox

Member
 
Join Date: Jan 2007
Posts: 46
#1: Sep 22 '07
Hi pals,

I would like to know how to display the FileName of the selected file in the textbox and open it using FileDialog property. I have imported the necessary reference from the library - Microsoft Office Object Library 10.0. Tks

Control:
TextBox - Display FileName
Button - Open

Code:

VBA:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command_Onclick() 
  2.  
  3.     Dim fd As FileDialog 
  4.     Dim varSelected As Variant 
  5.  
  6.     Set fd = Application.FileDialog(msopen....) 
  7.  
  8.     With fd 
  9.         .MultiSelect = True 
  10.         .Show 
  11.  
  12.         If . show = -1 Then 
  13.             For Each varSelected .SelectedItem 
  14.  
  15.             Next 
  16.         Else 
  17.             MsgBox "Cancel" 
  18.         End If 
  19.  
  20.     End With 
  21.  
  22. End Sub

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#2: Sep 22 '07

re: Using FileDialog property to open and display FileName in a textbox


Quote:

Originally Posted by technocraze

Hi pals,

I would like to know how to display the FileName of the selected file in the textbox and open it using FileDialog property. I have imported the necessary reference from the library - Microsoft Office Object Library 10.0. Tks

Control:
TextBox - Display FileName
Button - Open

Code:

VBA:
Private Sub Command_Onclick()

Dim fd As FileDialog
Dim varSelected As Variant

Set fd = Application.FileDialog(msopen....)

With fd
.MultiSelect = True
.Show

If . show = -1 Then
For Each varSelected .SelectedItem

Next
Else
MsgBox "Cancel"
End If

End With

End Sub

The following code will display a listing of multiple Files selected in a Text Box named txtDisplayFileNames using the FileDialog Object:
Expand|Select|Wrap|Line Numbers
  1. Const conVerticalOnly As Integer = 2
  2.  
  3. 'Declare a variable as a FileDialog object.
  4. Dim fd As FileDialog, strFilesSelected As String
  5.  
  6. Set fd = Application.FileDialog(msoFileDialogFilePicker)
  7.  
  8. 'Declare a variable to contain the path, it must be a Variant because
  9. 'of the For..Each construct requires it
  10. Dim vrtSelectedItem As Variant
  11.  
  12. With fd
  13.   .AllowMultiSelect = True
  14.   .Title = "Browse Files"
  15.      If .Show = -1 Then
  16.         For Each vrtSelectedItem In .SelectedItems
  17.           strFilesSelected = strFilesSelected & vrtSelectedItem & vbCrLf
  18.         Next vrtSelectedItem
  19.       Else
  20.       'The user pressed Cancel
  21.       End If
  22. End With
  23.  
  24. Set fd = Nothing
  25.  
  26. Me![txtDisplayFileNames].ScrollBars = conVerticalOnly
  27. Me![txtDisplayFileNames] = strFilesSelected
Member
 
Join Date: Jan 2007
Posts: 46
#3: Sep 22 '07

re: Using FileDialog property to open and display FileName in a textbox


tks for the prompt reply. However, the selected file cannot be opened when the prog is executed. It just display the name of the selected file in the textbox. What I need further is to open the selected file from the DirList. May I know how can that be achieved?

Logic of the prog:-
The prog should run in such a way, whereby the name of the selected file is displayed in the textbox as well as open upon execution. Tks


Quote:

Originally Posted by ADezii

The following code will display a listing of multiple Files selected in a Text Box named txtDisplayFileNames using the FileDialog Object:

Expand|Select|Wrap|Line Numbers
  1. Const conVerticalOnly As Integer = 2
  2.  
  3. 'Declare a variable as a FileDialog object.
  4. Dim fd As FileDialog, strFilesSelected As String
  5.  
  6. Set fd = Application.FileDialog(msoFileDialogFilePicker)
  7.  
  8. 'Declare a variable to contain the path, it must be a Variant because
  9. 'of the For..Each construct requires it
  10. Dim vrtSelectedItem As Variant
  11.  
  12. With fd
  13.   .AllowMultiSelect = True
  14.   .Title = "Browse Files"
  15.      If .Show = -1 Then
  16.         For Each vrtSelectedItem In .SelectedItems
  17.           strFilesSelected = strFilesSelected & vrtSelectedItem & vbCrLf
  18.         Next vrtSelectedItem
  19.       Else
  20.       'The user pressed Cancel
  21.       End If
  22. End With
  23.  
  24. Set fd = Nothing
  25.  
  26. Me![txtDisplayFileNames].ScrollBars = conVerticalOnly
  27. Me![txtDisplayFileNames] = strFilesSelected

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#4: Sep 22 '07

re: Using FileDialog property to open and display FileName in a textbox


Quote:

Originally Posted by technocraze

tks for the prompt reply. However, the selected file cannot be opened when the prog is executed. It just display the name of the selected file in the textbox. What I need further is to open the selected file from the DirList. May I know how can that be achieved?

Logic of the prog:-
The prog should run in such a way, whereby the name of the selected file is displayed in the textbox as well as open upon execution. Tks

  1. Are you trying to Open Single or Multiple Files?
  2. What File Type Extensions do you wish to be limited to? (What File Types are to opened)?
  3. Are you sure you want to set MultiSelect = True.
  4. What exactly are you trying to accomplish?
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#5: Sep 22 '07

re: Using FileDialog property to open and display FileName in a textbox


Quote:

Originally Posted by technocraze

tks for the prompt reply. However, the selected file cannot be opened when the prog is executed. It just display the name of the selected file in the textbox. What I need further is to open the selected file from the DirList. May I know how can that be achieved?

Logic of the prog:-
The prog should run in such a way, whereby the name of the selected file is displayed in the textbox as well as open upon execution. Tks

  1. Are you trying to Open Single or Multiple Files?
  2. What File Type Extensions do you wish to be limited to? (What File Types are to opened)?
  3. Are you sure you want to set MultiSelect = True?
  4. What exactly are you trying to accomplish?
Member
 
Join Date: Jan 2007
Posts: 46
#6: Sep 23 '07

re: Using FileDialog property to open and display FileName in a textbox


Hi,

What I want to achieve are:

Upon the button click event, the "Browse file" window will be loaded. When I clicked on the "Open" button, the chosen file should be appearing on the "FileName" Listbox, and the file should open. Eventually, the filename should display on the textbox. In addition, the prog should also open any files in the network drive.

What has been accomplished so far is only displaying the selected fileName on the TextBox. tks
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#7: Sep 24 '07

re: Using FileDialog property to open and display FileName in a textbox


Quote:

Originally Posted by technocraze

Hi,

What I want to achieve are:

Upon the button click event, the "Browse file" window will be loaded. When I clicked on the "Open" button, the chosen file should be appearing on the "FileName" Listbox, and the file should open. Eventually, the filename should display on the textbox. In addition, the prog should also open any files in the network drive.

What has been accomplished so far is only displaying the selected fileName on the TextBox. tks

It seems as though you need the capability to select and Open Files with any File Type Extension (no restrictions on Files). Is this assumption correct? I'm specifically asking you this, because this can easily be done, but involves accessing the API, and I wanted to make sure that you are not in over your head. If this is what you are requesting, just let me know, and I'll gladly post the code with some explanation. Do you need the capability to Open multiple Files at a time (MultiSelect = True) or only allow one File to be Opened at a time (MultiSelect = False). Opening a half dozen or so Files along with the Applications associated with them can get to be real ugly real fast!
Member
 
Join Date: Jan 2007
Posts: 46
#8: Sep 24 '07

re: Using FileDialog property to open and display FileName in a textbox


Tks for your prompt reply once again.

Yes, u are right!

May I know whether it is possible to achieve just using vb and not API. Well, opening a single file should be fine. In addition, may I know whether there is such Function or property called "DirList".


Quote:

Originally Posted by ADezii

It seems as though you need the capability to select and Open Files with any File Type Extension (no restrictions on Files). Is this assumption correct? I'm specifically asking you this, because this can easily be done, but involves accessing the API, and I wanted to make sure that you are not in over your head. If this is what you are requesting, just let me know, and I'll gladly post the code with some explanation. Do you need the capability to Open multiple Files at a time (MultiSelect = True) or only allow one File to be Opened at a time (MultiSelect = False). Opening a half dozen or so Files along with the Applications associated with them can get to be real ugly real fast!

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#9: Sep 24 '07

re: Using FileDialog property to open and display FileName in a textbox


Quote:

Originally Posted by technocraze

Tks for your prompt reply once again.

Yes, u are right!

May I know whether it is possible to achieve just using vb and not API. Well, opening a single file should be fine. In addition, may I know whether there is such Function or property called "DirList".

Quote:
May I know whether it is possible to achieve just using vb and not API. Well, opening a single file should be fine. In addition, may I know whether there is such Function or property called "DirList".
  1. You will be opening Files with no restrictions whatsoever on their Types (.???). To my way of thinking, there is no way to accomplish this but to Open/Execute the Files by nature of their File Extensions. The Server Application associated with the Extension needs to be executed and the File loaded within it. This can be done exclusively within the confines of VBA, but a single API Call wrapped in a Sub-Procedure is still needed.
  2. Going the single File route is an excellent idea.
  3. There is no such Function or Property called DirList. What you may be referring to is either the Dir() Function in VB/VBA, or the DirListBox Control available in Visual Basic.
  4. Give me a little time to get the applicable code together and tie it all in to the FileDialog code that has already been posted.
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#10: Sep 24 '07

re: Using FileDialog property to open and display FileName in a textbox


Quote:

Originally Posted by technocraze

Tks for your prompt reply once again.

Yes, u are right!

May I know whether it is possible to achieve just using vb and not API. Well, opening a single file should be fine. In addition, may I know whether there is such Function or property called "DirList".

tecnocraze, here is the solution to your problem. I narrowed it down to 3 steps - follow them exactly, and you'll be OK. The code has been thoroughly debugged and is fully operational as is. I would strongly suggest you not try to modify the Function Procedure or actual call, since it does execute an API Function. Well, here goes:
  1. Declare the ShellExecute() API Function in a Standard Code Module. This is what does the 'dirty work' and actually Opens the File based solely on its Extension.
    Expand|Select|Wrap|Line Numbers
    1. Declare Function ShellExecute Lib "shell32.dll" _
    2. Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _
    3. ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    4.  
  2. Copy and Paste the Public Execute_Program() Function into a Standard Code Module. This Function is a 'Wrapper' for the actual ShellExecute() Function. It essentially makes the API Function transparent and shields the User from its intricacies. As you can see, it accepts only 3 Arguments, 2 of which you probably will never use. I would strongly suggest you keep the Error Evaluation code in place and not delete it, since it may prove invaluable should you experience any problems Opening Files.
    Expand|Select|Wrap|Line Numbers
    1. Public Function Execute_Program(ByVal strFilePath As String, _
    2.     ByVal strParms As String, ByVal strDir As String) _
    3.     As Boolean
    4.  
    5. 'run program ' <R6>
    6. Dim hwndProgram As Integer
    7. hwndProgram = ShellExecute(0, "Open", strFilePath, strParms, strDir, 3)     '3 ==> Show Maximized
    8.  
    9. 'evaluate errors (if any)
    10. Select Case (hwndProgram)
    11.   Case 0
    12.     MsgBox "Insufficient system memory or corrupt program file.", 0, "Error running " & strFilePath
    13.       Execute_Program = False
    14.         Exit Function
    15.   Case 2
    16.     MsgBox "File not found.", 0, "Error running " & strFilePath
    17.       Execute_Program = False
    18.         Exit Function
    19.   Case 3
    20.     MsgBox "Invalid path.", 0, "Error running " & strFilePath
    21.       Execute_Program = False
    22.         Exit Function
    23.   Case 5
    24.     MsgBox "Sharing or Protection Error.", 0, "Error running " & strFilePath
    25.       Execute_Program = False
    26.         Exit Function
    27.   Case 6
    28.     MsgBox "Separate data segments are required for each task.", 0, "Error running " & strFilePath
    29.       Execute_Program = False
    30.         Exit Function
    31.   Case 8
    32.     MsgBox "Insufficient memory to run the program.", 0, "Error running " & strFilePath
    33.       Execute_Program = False
    34.         Exit Function
    35.   Case 10
    36.     MsgBox "Incorrect Windows version.", 0, "Error running " & strFilePath
    37.       Execute_Program = False
    38.         Exit Function
    39.   Case 11
    40.     MsgBox "Invalid program file.", 0, "Error running " & strFilePath
    41.       Execute_Program = False
    42.         Exit Function
    43.   Case 12
    44.     MsgBox "Program file requires a different operating system.", 0, "Error running " & strFilePath
    45.       Execute_Program = False
    46.         Exit Function
    47.   Case 13
    48.     MsgBox "Program requires MS-DOS 4.0.", 0, "Error running " & strFilePath
    49.       Execute_Program = False
    50.         Exit Function
    51.   Case 14
    52.     MsgBox "Unknown program file type.", 0, "Error running " & strFilePath
    53.       Execute_Program = False
    54.         Exit Function
    55.   Case 15
    56.     MsgBox "Windows program does not support protected memory mode.", 0, "Error running " & strFilePath
    57.       Execute_Program = False
    58.         Exit Function
    59.   Case 16
    60.     MsgBox "Invalid use of data segments when loading a second instance of a program.", 0, "Error running " & strFilePath
    61.       Execute_Program = False
    62.         Exit Function
    63.   Case 19
    64.     MsgBox "Attempt to run a compressed program file.", 0, "Error running " & strFilePath
    65.       Execute_Program = False
    66.         Exit Function
    67.   Case 20
    68.     MsgBox "Invalid dynamic link library.", 0, "Error running " & strFilePath
    69.       Execute_Program = False
    70.         Exit Function
    71.   Case 21
    72.     MsgBox "Program requires Windows 32-bit extensions.", 0, "Error running " & strFilePath
    73.       Execute_Program = False
    74.         Exit Function
    75.   Case Else
    76. End Select
    77.   'All is well if we get to this point
    78.   Execute_Program = True
    79. End Function
  3. Last but not least, I've made several modifications to the actual code which Opens the File Dialog, allows you to select a File, then Opens/Executes it. Rather than pinpoint the changes, eliminate the original code and substitute this modified version. This code will Open the Office File Dialog Box, and once a selection has been made and OK-ed by the User, the File will be Opened in the 'Registered' Application that is associated with the File's Extension.
    Expand|Select|Wrap|Line Numbers
    1. Const conVerticalOnly As Integer = 2
    2.  
    3. 'Declare a variable as a FileDialog object.
    4. Dim fd As FileDialog, strFilesSelected As String, blnRetVal As Boolean
    5.  
    6. Set fd = Application.FileDialog(msoFileDialogFilePicker)
    7.  
    8. 'Declare a variable to contain the path, it must be a Variant because
    9. 'of the For..Each construct requires it
    10. Dim vrtSelectedItem As Variant
    11.  
    12. With fd
    13.   .AllowMultiSelect = False
    14.   .Title = "Browse Files"
    15.      If .Show = -1 Then
    16.         For Each vrtSelectedItem In .SelectedItems
    17.           'Now, will only be 1 since MultiSelect = False
    18.           strFilesSelected = vrtSelectedItem
    19.           blnRetVal = Execute_Program(strFilesSelected, "", "")
    20.         Next vrtSelectedItem
    21.       Else
    22.       'The user pressed Cancel
    23.       End If
    24. End With
    25.  
    26. Set fd = Nothing
    27.  
    28. Me![txtDisplayFileNames].ScrollBars = conVerticalOnly
    29. Me![txtDisplayFileNames] = strFilesSelected
  4. Good Luck and let me know how you make out.
Member
 
Join Date: Jan 2007
Posts: 46
#11: Sep 25 '07

re: Using FileDialog property to open and display FileName in a textbox


tks for your prompt reply once again. Very much appreciated
Member
 
Join Date: Jan 2007
Posts: 46
#12: Sep 25 '07

re: Using FileDialog property to open and display FileName in a textbox


tks for your prompt reply. Very much apppreciated
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#13: Sep 25 '07

re: Using FileDialog property to open and display FileName in a textbox


Quote:

Originally Posted by technocraze

tks for your prompt reply. Very much apppreciated

Not a problem. It can easily be adapted for multiple file openings also, but I do not think that this would be advisable.
Member
 
Join Date: Jan 2007
Posts: 46
#14: Sep 26 '07

re: Using FileDialog property to open and display FileName in a textbox


It works perfectly well. Great! Tks
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#15: Sep 28 '07

re: Using FileDialog property to open and display FileName in a textbox


Quote:

Originally Posted by technocraze

It works perfectly well. Great! Tks

Glad it worked out for you.
Newbie
 
Join Date: Jul 2009
Posts: 2
#16: Jul 6 '09

re: Using FileDialog property to open and display FileName in a textbox


Hi,
could you tell me how to include all these codes (these 3 steps) into Access 2003 database?
Thanks in advance

Quote:

Originally Posted by technocraze View Post

tks for your prompt reply once again. Very much appreciated

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#17: Jul 6 '09

re: Using FileDialog property to open and display FileName in a textbox


Quote:

Originally Posted by sajtovi007 View Post

Hi,
could you tell me how to include all these codes (these 3 steps) into Access 2003 database?
Thanks in advance

What you are attempting is called 'Hijacking' a Thread and is not permitted in this Forum. All the code you need has already been posted, if you are still confused, please create another Thread explaining your needs, and we will be happy to Reply.
Reply