Solved

Multiple file types filtered with OPENFILENAME

Posted on 2011-09-27
4
472 Views
Last Modified: 2012-05-12
Hello,

I'm using the code below to call the OPENFILENAME dialog.  I need to filter xls and xlsx files, but it can only do one at a time.  When I put both extentions in as below nothing comes up.

Any ideas?  I'm working in Access 2010.

 sFilter = "All Excel Files" & Chr(0) & "*.xls, *.XLSX" & Chr(0)

    Dim OpenFile As OPENFILENAME
      Dim lReturn As Long
      Dim sFilter As String
      OpenFile.lStructSize = Len(OpenFile)
      OpenFile.hwndOwner = Me.Hwnd
      'OpenFile.hInstance = App.hInstance

        sFilter = "Excel Files (*.xls)" & Chr(0) & "*.xls, *.XLSX" & 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 = "S:\clients\SENIOR LIVING\Minnesota\MN 2010\Census\Family Lists"
      'OpenFile.lpstrInitialDir = "S:\VR\1VR\VR - Ron\Development\MN_F_04122\Family_List_Test_Files"
      OpenFile.lpstrInitialDir = ""
      OpenFile.lpstrTitle = "Select the import file..."
      OpenFile.flags = 0
      lReturn = GetOpenFileName(OpenFile)
      If lReturn = 0 Then
         'MsgBox "The User pressed the Cancel Button"
      Else
         'MsgBox "The user Chose " & Trim(OpenFile.lpstrFile)
         subGetFile = RTrim(OpenFile.lpstrFile)
      End If

Open in new window

0
Comment
Question by:eshurak
  • 2
4 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 250 total points
ID: 36714106
Does this work?

sFilter = "Excel Files (*.xls)" & Chr(0) & "*.xls*" & Chr(0)
0
 
LVL 74

Assisted Solution

by:Jeffrey Coachman
Jeffrey Coachman earned 250 total points
ID: 36714659
Try it like this perhaps:

    sFilter = "All Excel Files (*.xls, *.xlsx)" & Chr(0) & "*.xls; *.xlsx" & Chr(0)

The first segment is what will displayed on the "One Line"
    "All Excel Files (*.xls, *.xlsx)"

The second segment is the actual filter(s) (separated by semicolons...
    "*.xls; *.xlsx"

Chr(0) is just a Null (I think...)


JeffCoachman
0
 
LVL 3

Author Comment

by:eshurak
ID: 36718618
Thanks guys.  Both solutions work.  

chr(0) is null.  It's used as the delimiter in case you want the user to be able to choose from several filters.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 36772111
great
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you how to use shortcut menus in the Access run-time environment.
My experience with Windows 10 over a one year period and suggestions for smooth operation
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question