checking file extention before opening

hilyete

Active Member
Joined
Aug 19, 2009
Messages
293
My project includes a button that opens an API file search window that allows the user to select and open a file for data import. I want to include code that will check to see if the file is a csv file, (as others will not work for this project). What code do I need to add that would check the extention to make sure it is a csv file and end the sub routine if it isn't?

Thanks

Tom
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi, :)

maybe you can do something with this code? Only CSV files can be selected:

Code:
Option Explicit
Sub Main()
    Dim varFile As Variant
    On Error GoTo Fin
    varFile = Application.GetOpenFilename("CSV files,*.csv", 1, "Select", , False)
    If Not varFile <> False Then Exit Sub
    ' your code here...
Fin:
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for responding,

I can't seem to get that to work for me. Here's the function as it is in the code that opens the API file selection window: (This works, I just need to limit the files that can be chosen to cvs files).

Code:
Function GetFileName(strPath As String)
Dim strFilter As String
Dim lngFlags As Long
strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.xls)")
GetFileName = ahtCommonFileOpenSave(InitialDir:=strPath, _
    Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
    DialogTitle:="Please select file to import")
     
End Function

And here is the code in my routine that puts the data from the opened file into a PowerPoint presentation.

Code:
sFileName = GetFileName("c:\")
 
iFileNum = FreeFile()
Open sFileName For Input As iFileNum
' Use the current presentation
With ActivePresentation
Do While Not EOF(iFileNum)
Line Input #iFileNum, cSpanish
Line Input #iFileNum, cEnglish
.Slides(1).Duplicate
Set oSl = .Slides(Count + 1)

I'm not sure where I'd put your code in. I tried it in both of the above but I get a "method or data member not found" message with the "GetOpenFileName" highlighted. Any ideas?

Tom
 
Upvote 0
I don't use that API, but you're setting the filter for Excel files ...

Code:
strFilter = ahtAddFilterItem(strFilter, [COLOR=red]"Excel Files (*.xls)"[/COLOR])
 
Upvote 0
Thanks shg,

Yeah, it's a head scratcher (for me anyway)... The filter does come up on the file selection window, but it doesn't do anything. It doesn't filter out other files, and it doesn't warn against opening one that isn't. I changed it to cvs to test it out... nothing.
 
Upvote 0
Dunno; again, I don't use the API.

I'd use Case Germany's suggestion.
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top