![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Board Regular
Join Date: Feb 2002
Location: Wisconsin - USA
Posts: 62
|
I have a need to use the GetOpenFileName method, to select a file.
The default directory where the files are stored have different names.... 123_ABC.txt 333_ABC.txt 999_WYZ.txt I can use the FileFilter property to narrow down *.txt files. But I need to only display the _ABC.txt files. I can get it to work only manually through the dialog...i.e. type in: 'ABC.txt' should filter the list in the dialog to: 123_ABC.txt & 333_ABC.txt Does anyone know how to programatically match the file name just like the "File name" option in the dialog box itself? Thanks. |
|
|
|
|
|
#2 |
|
Board Regular
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
|
Hi
This question was answered well by Ivan?,I think, in the past week or so. API routine. Do a search. Tom |
|
|
|
|
|
#3 |
|
Board Regular
Join Date: Feb 2002
Location: Wisconsin - USA
Posts: 62
|
Thanks> I think I found it...
http://mrexcel.com/board/viewtopic.p...c=5262&forum=2 Option Explicit Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _ "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long Private Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As String nMaxCustFilter As Long nFilterIndex As Long lpstrFile As String nMaxFile As Long lpstrFileTitle As String nMaxFileTitle As Long lpstrInitialDir As String lpstrTitle As String flags As Long nFileOffset As Integer nFileExtension As Integer lpstrDefExt As String lCustData As Long lpfnHook As Long lpTemplateName As String End Type Private Sub Command1_Click() Dim OpenFile As OPENFILENAME Dim lReturn As Long Dim sFilter As String OpenFile.lStructSize = Len(OpenFile) '// Define your wildcard string here sFilter = "my Files (*pid.isd.*)" & Chr(0) & "*pid.isd.*" & Chr(0) OpenFile.lpstrFilter = sFilter OpenFile.nFilterIndex = 1 OpenFile.lpstrFile = String(257, 0) OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1 OpenFile.lpstrFileTitle = OpenFile.lpstrFile OpenFile.nMaxFileTitle = OpenFile.nMaxFile OpenFile.lpstrInitialDir = "C:\" OpenFile.lpstrTitle = "My File Open" OpenFile.flags = 0 lReturn = GetOpenFileName(OpenFile) If lReturn = 0 Then '// Do your thing Else '// Do your thing End If |
|
|
|
|
|
#4 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
|
Hi
This code was in reply to another filter Post if you get stuck applying the filter to your situation and/or getting it to open via the default application or excel. |
|
|
|
|
|
#5 |
|
Board Regular
Join Date: Feb 2002
Location: Wisconsin - USA
Posts: 62
|
Hi Ivan,
Yes, that code doesn't quite work. When we run it, we see the following in the Files of Type drop-down list: my Files(*_ABC.txt.*) We also tried it without the last ".", but it just displayed: my Files(*_ABC.txt*) However, no files defined of that type display. Any suggestions would be great. Perhaps this filter should be applied to the "File Name" entry box instead? |
|
|
|
|
|
#6 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
|
Quote:
sFilter = "my Files (*ABC.txt)" & Chr(0) & "*ABC.txt" & Chr(0) |
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|