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
16 17516
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: - Const conVerticalOnly As Integer = 2
-
-
'Declare a variable as a FileDialog object.
-
Dim fd As FileDialog, strFilesSelected As String
-
-
Set fd = Application.FileDialog(msoFileDialogFilePicker)
-
-
'Declare a variable to contain the path, it must be a Variant because
-
'of the For..Each construct requires it
-
Dim vrtSelectedItem As Variant
-
-
With fd
-
.AllowMultiSelect = True
-
.Title = "Browse Files"
-
If .Show = -1 Then
-
For Each vrtSelectedItem In .SelectedItems
-
strFilesSelected = strFilesSelected & vrtSelectedItem & vbCrLf
-
Next vrtSelectedItem
-
Else
-
'The user pressed Cancel
-
End If
-
End With
-
-
Set fd = Nothing
-
-
Me![txtDisplayFileNames].ScrollBars = conVerticalOnly
-
Me![txtDisplayFileNames] = strFilesSelected
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 The following code will display a listing of multiple Files selected in a Text Box named txtDisplayFileNames using the FileDialog Object: - Const conVerticalOnly As Integer = 2
-
-
'Declare a variable as a FileDialog object.
-
Dim fd As FileDialog, strFilesSelected As String
-
-
Set fd = Application.FileDialog(msoFileDialogFilePicker)
-
-
'Declare a variable to contain the path, it must be a Variant because
-
'of the For..Each construct requires it
-
Dim vrtSelectedItem As Variant
-
-
With fd
-
.AllowMultiSelect = True
-
.Title = "Browse Files"
-
If .Show = -1 Then
-
For Each vrtSelectedItem In .SelectedItems
-
strFilesSelected = strFilesSelected & vrtSelectedItem & vbCrLf
-
Next vrtSelectedItem
-
Else
-
'The user pressed Cancel
-
End If
-
End With
-
-
Set fd = Nothing
-
-
Me![txtDisplayFileNames].ScrollBars = conVerticalOnly
-
Me![txtDisplayFileNames] = strFilesSelected
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
- Are you trying to Open Single or Multiple Files?
- What File Type Extensions do you wish to be limited to? (What File Types are to opened)?
- Are you sure you want to set MultiSelect = True.
- What exactly are you trying to accomplish?
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
- Are you trying to Open Single or Multiple Files?
- What File Type Extensions do you wish to be limited to? (What File Types are to opened)?
- Are you sure you want to set MultiSelect = True?
- What exactly are you trying to accomplish?
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
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!
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".
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!
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".
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".
- 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.
- Going the single File route is an excellent idea.
- 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.
- 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.
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: - 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.
- 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 Long) As Long
-
- 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.
- Public Function Execute_Program(ByVal strFilePath As String, _
-
ByVal strParms As String, ByVal strDir As String) _
-
As Boolean
-
-
'run program ' <R6>
-
Dim hwndProgram As Integer
-
hwndProgram = ShellExecute(0, "Open", strFilePath, strParms, strDir, 3) '3 ==> Show Maximized
-
-
'evaluate errors (if any)
-
Select Case (hwndProgram)
-
Case 0
-
MsgBox "Insufficient system memory or corrupt program file.", 0, "Error running " & strFilePath
-
Execute_Program = False
-
Exit Function
-
Case 2
-
MsgBox "File not found.", 0, "Error running " & strFilePath
-
Execute_Program = False
-
Exit Function
-
Case 3
-
MsgBox "Invalid path.", 0, "Error running " & strFilePath
-
Execute_Program = False
-
Exit Function
-
Case 5
-
MsgBox "Sharing or Protection Error.", 0, "Error running " & strFilePath
-
Execute_Program = False
-
Exit Function
-
Case 6
-
MsgBox "Separate data segments are required for each task.", 0, "Error running " & strFilePath
-
Execute_Program = False
-
Exit Function
-
Case 8
-
MsgBox "Insufficient memory to run the program.", 0, "Error running " & strFilePath
-
Execute_Program = False
-
Exit Function
-
Case 10
-
MsgBox "Incorrect Windows version.", 0, "Error running " & strFilePath
-
Execute_Program = False
-
Exit Function
-
Case 11
-
MsgBox "Invalid program file.", 0, "Error running " & strFilePath
-
Execute_Program = False
-
Exit Function
-
Case 12
-
MsgBox "Program file requires a different operating system.", 0, "Error running " & strFilePath
-
Execute_Program = False
-
Exit Function
-
Case 13
-
MsgBox "Program requires MS-DOS 4.0.", 0, "Error running " & strFilePath
-
Execute_Program = False
-
Exit Function
-
Case 14
-
MsgBox "Unknown program file type.", 0, "Error running " & strFilePath
-
Execute_Program = False
-
Exit Function
-
Case 15
-
MsgBox "Windows program does not support protected memory mode.", 0, "Error running " & strFilePath
-
Execute_Program = False
-
Exit Function
-
Case 16
-
MsgBox "Invalid use of data segments when loading a second instance of a program.", 0, "Error running " & strFilePath
-
Execute_Program = False
-
Exit Function
-
Case 19
-
MsgBox "Attempt to run a compressed program file.", 0, "Error running " & strFilePath
-
Execute_Program = False
-
Exit Function
-
Case 20
-
MsgBox "Invalid dynamic link library.", 0, "Error running " & strFilePath
-
Execute_Program = False
-
Exit Function
-
Case 21
-
MsgBox "Program requires Windows 32-bit extensions.", 0, "Error running " & strFilePath
-
Execute_Program = False
-
Exit Function
-
Case Else
-
End Select
-
'All is well if we get to this point
-
Execute_Program = True
-
End Function
- 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.
- Const conVerticalOnly As Integer = 2
-
-
'Declare a variable as a FileDialog object.
-
Dim fd As FileDialog, strFilesSelected As String, blnRetVal As Boolean
-
-
Set fd = Application.FileDialog(msoFileDialogFilePicker)
-
-
'Declare a variable to contain the path, it must be a Variant because
-
'of the For..Each construct requires it
-
Dim vrtSelectedItem As Variant
-
-
With fd
-
.AllowMultiSelect = False
-
.Title = "Browse Files"
-
If .Show = -1 Then
-
For Each vrtSelectedItem In .SelectedItems
-
'Now, will only be 1 since MultiSelect = False
-
strFilesSelected = vrtSelectedItem
-
blnRetVal = Execute_Program(strFilesSelected, "", "")
-
Next vrtSelectedItem
-
Else
-
'The user pressed Cancel
-
End If
-
End With
-
-
Set fd = Nothing
-
-
Me![txtDisplayFileNames].ScrollBars = conVerticalOnly
-
Me![txtDisplayFileNames] = strFilesSelected
- Good Luck and let me know how you make out.
tks for your prompt reply once again. Very much appreciated
tks for your prompt reply. Very much apppreciated
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.
It works perfectly well. Great! Tks
It works perfectly well. Great! Tks
Glad it worked out for you.
Hi,
could you tell me how to include all these codes (these 3 steps) into Access 2003 database?
Thanks in advance @technocraze @sajtovi007
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.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
112 posts
views
Thread by Andy |
last post: by
|
2 posts
views
Thread by seberino |
last post: by
|
2 posts
views
Thread by theWizard1 |
last post: by
|
reply
views
Thread by Petr Jakes |
last post: by
| | | | | | | | | | | | | | | |