473,473 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is it possiple to use file dialog box to search for a single file in a folder?

61 New Member
I created a userform that through user input will get a directory i.e.("C:\some folder\some file.xxx") up, and opens the folder where the file exists with a file dialog box. From here I would like the file dialog box to only show the "some file.xxx" that the user selected, not every single file that is in the folder.

Is this possible?

I'm using this simple code
Expand|Select|Wrap|Line Numbers
  1. ChDir (directory)
  2.     strDocument = Application.GetOpenFilename(Filter, 1, Title)
  3. If Len(strDocument) < 6 Then Exit Function
  4.     ActiveWorkbook.FollowHyperlink strDocument 'Open document
  5.  
i'm using vba in excel 2007
What I have works, i'm just trying to refine my program.
If anyone can help or has any other ideas to accomplish this i'd appreciate it.

thx
RRR
Feb 3 '11 #1
8 2219
Guido Geurs
767 Recognized Expert Contributor
This works in Excel 2003.(maybe You have to modify it for 2007)
Use a textbox to enter the filter and use .Filename

Expand|Select|Wrap|Line Numbers
  1. Private Sub CommandButton1_Click()
  2.    On Error GoTo Cancel_Sub
  3.    With CommonDialog1
  4.       .CancelError = True
  5.       .Filename = TextBox1.Text
  6.       .InitDir = Environ$("USERPROFILE") & "\My Documents"
  7.       .ShowOpen
  8.    End With
  9. Exit Sub
  10. Cancel_Sub:
  11. End Sub
Feb 4 '11 #2
Rodney Roe
61 New Member
Once I get past the with block it give my an error 424 object required at line 4.
Feb 4 '11 #3
Guido Geurs
767 Recognized Expert Contributor
I have test the code in 2007 with no errors!
Attached is my workbook with macro.
Feb 4 '11 #4
Rodney Roe
61 New Member
Sorry, i'm a little new with the commondialog. Ok, my problem was that I didn't have the commondialog control. Where do you find that? Appart from that, i have a file list.xls how do I get just that file to show up in the dialog box? I see that the text box adds the file name into the file name box in the dialog, but all the files are still in the dialog box. What I need this for is so that I can easily search out a file and be able to open it or drag and drop into another program.

thanks for your help.
RRR
Feb 4 '11 #5
Guido Geurs
767 Recognized Expert Contributor
To insert a ComDiaBox, just click RMB on the "Toolbox" and select the component in the "Additional Controls" window (see attached GIFs).

The Textbox is used to enter the FILTER for the ComDiaBox.
If You enter "test*" You will see all the files starting with "test".

You say You have a list with the filenames.
If the data is the exact filename, You don't have to use ComDiaBox for opening the file!
Just use a file systemObject(FSO) : see atachment.
Or go to the form with the filter and enter the "test*" to see all the files starting with "test".
Attached Images
File Type: gif Add component 1.GIF (9.6 KB, 426 views)
File Type: gif Add component 2.GIF (18.5 KB, 261 views)
Attached Files
File Type: zip Is it possiple to use file dialog box to search for a single file in a folder_v2_.zip (15.6 KB, 59 views)
Feb 5 '11 #6
Rodney Roe
61 New Member
Ok I think I got it now. I just needed to use the "*" to trigger it.

Last question how do I get it to open the file? Does it return a string that I have to open with a hyperlink or is there another way. I can right click on the file and select a program to open with, but when I press the open button or double click on the file it doesn't do anything. Am I supposed to programatically tell it what program to use to open with? I've searched the commondialog on the internet but everything i get is a little above my head right now.

To answer your question about the filename, in my program the user selects a file name and depending on the file name it programatically selects the folder it is in. From there the user should be able to open it or drag it into another program that we're using. Hence the reason why I need the commond dialog. I need both those options available. Thanks for you help I've learned lots.

RRR
Feb 7 '11 #7
Guido Geurs
767 Recognized Expert Contributor
ComDia does not open the file, it put only the path and filename in the var= CommonDialog1.FileName

In the macro I have attached I set the path and filename fixt to UserEnvironment and the filename from the sheet:

Expand|Select|Wrap|Line Numbers
  1. FILENAME = Environ$("USERPROFILE") & "\My Documents\" & ActiveCell.Value
If you want to use the selected filename from the CommonDialog1 then the code will be: (see attachment)

Expand|Select|Wrap|Line Numbers
  1.         RET = Shell("notepad.exe " & .FILENAME, vbNormalFocus)
For opening the right folder:
in the attachment the Dir is set with:

Expand|Select|Wrap|Line Numbers
  1.       .InitDir = Environ$("USERPROFILE") & "\My Documents"
For your program it will be something like this:

Expand|Select|Wrap|Line Numbers
  1.       .InitDir = PATH_FROM_MY_PROGRAM
If you want the path:

Expand|Select|Wrap|Line Numbers
  1. PATH = Left(.Filename, InStrRev(.Filename, "\") - 1)
Feb 8 '11 #8
Rodney Roe
61 New Member
Thanks Guido you've been a great help. Here's what I came up with.
I have two different options.
Expand|Select|Wrap|Line Numbers
  1. With CommonDialog1
  2.    .CancelError = True
  3.    .Filename = TextBox1.Text & "*"
  4.    .InitDir = "insert Path"
  5.    .ShowOpen
  6.   If Dir(.Filename) <> "" Then
  7.     ActiveWorkbook.FollowHyperlink .Filename
  8.   Else
  9.     MsgBox "File Not Found."
  10.   End If
  11. End With
  12.  
or this:
Expand|Select|Wrap|Line Numbers
  1. With CommonDialog1
  2.    .CancelError = True
  3.    .Filename = TextBox1.Text & "*"
  4.    .InitDir = "insert Path"
  5.    .ShowOpen
  6.   If FSO.FileExists(.Filename) Then
  7.      RET = Shell("executable program to open file with " & .Filename, vbNormalFocus)
  8.      Exit Sub
  9.   Else
  10.      MsgBox "File not found"
  11.   End If
  12. End With
  13.  
this works, but with the hyperlink I loose some functionality of the file when opened i.e. can't "Save As", and with the shell it opens a new instance of the program every time instead of using the one I already have open. I'll have to do some more research on these. thanks for your help.
Feb 8 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Dan Kean | last post by:
Hi all, how can I bring a file browser up to select a file then send the path to a textbox and display it (will be image file) also can I limit extensions to *.bmp etc? thanx for any help!...
3
by: GrantS | last post by:
I am needing to permit users to select files using the 'Open file' common dialog box. The format of the names of the files are along the names of: GB00((&X.001.jpg I am not able to show these...
13
by: Kevin Walzer | last post by:
Which of the Windows/Unix package builders for Python applications is capable of creating single-file executables? I'm thinking of: 1. py2exe 2. Mcmillan Installer/PyInstaller 3. cxfreeze ...
4
by: Jonny | last post by:
Hello Group How do I open a Save File Dialog from an ASPX page behind a browse button? Any help would be fantastic!! I am using ASP.NET 1.1 using VB.NET as the coding language TIA
2
by: shwetu | last post by:
Need a file dialog box in asp.net 2.0 which selects folder not single file
3
by: Highlander | last post by:
Hello all. Consider the following HTA: <HTML> <HEAD> <TITLE></TITLE></HEAD> <BODY> <SCRIPT LANGUAGE="VBScript"> Sub ButBrowse_onclick() '-- show browse window and Get file path:
1
by: SAL | last post by:
Hello, Has anyone use the Open File Dialog Box in ASP.net? I know you can add the HTML File Field control to your ASP page and it will open the File Dialog Box for you. However, I would like...
1
by: archana | last post by:
Hi all, I am confuse regarding concept of code behind model and single file model.. I have heard that single file model compiles dynamically. suppose i have application in which i am having...
3
by: vedika | last post by:
hi, I created one application in which there is export functionality. I am using sqlCE as database . When user click on "export" a save file dialog appeare and file saved in html format from...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.