Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default Finding File Extension from Full Filename (VBA) (2007)

Is there a better way of extracting the file extension from a full filename?

What I've got works BUT thought there may be a better way of writing it ...
Particularly when some people have some very weird filenames with lots of "."
and my current code only caters for 4 "."! You would think that would be
enough but.

New to VBA but trying my best :o)

Column A contains full filename:

..bmp
emailssent2003-4, 4-5.pst
A Person and B Person. Workshop C. Fire Safety Guidance..ppt
File With Dot No Extension.

Column B contains:

=FileExt($A1,"EXT")
=FileExt($A2,"EXT")
=FileExt($A3,"EXT")
=FileExt($A$,"EXT")

Function FileExt(Filename, part) As String
FileExt = "Error"
myStr = Mid(Filename, 1)
EXT = "N/A"
If Len(myStr) = 0 Then: EXT = "": Exit Function
xxx = Split(myStr, ".", 4, vbTextCompare)
If UBound(xxx) = 3 Then EXT = xxx(3)
If UBound(xxx) = 2 Then EXT = xxx(2)
If UBound(xxx) = 1 Then EXT = xxx(1)
If UBound(xxx) = 0 Then EXT = xxx(0)
If UBound(xxx) = 0 Then EXT = ""
If Left(EXT, 1) = "." Then EXT = Right(EXT, Len(EXT) - 1)
Select Case UCase(part)
Case "EXT": FileExt = EXT
Case Else: Filename = "part '" & part & "' not valid"
End Select
End Function



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Finding File Extension from Full Filename (VBA) (2007)

See how I do it here (Example 2
http://www.rondebruin.nl/mail/folder1/mail1.htm


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 91
Default Finding File Extension from Full Filename (VBA) (2007)

On Jun 2, 11:45*pm, Charlie wrote:
Is there a better way of extracting the file extension from a full filename?

What I've got works BUT thought there may be a better way of writing it ....
Particularly when some people have some very weird filenames with lots of "."
and my current code only caters for 4 "."! You would think that would be
enough but.

New to VBA but trying my best :o)

Column A contains full filename:

.bmp
emailssent2003-4, 4-5.pst
A Person and B Person. Workshop C. Fire Safety Guidance..ppt
File With Dot No Extension.

Column B contains:

=FileExt($A1,"EXT")
=FileExt($A2,"EXT")
=FileExt($A3,"EXT")
=FileExt($A$,"EXT")

Function FileExt(Filename, part) As String
* FileExt = "Error"
* myStr = Mid(Filename, 1)
* EXT = "N/A"
* If Len(myStr) = 0 Then: EXT = "": Exit Function
* xxx = Split(myStr, ".", 4, vbTextCompare)
* If UBound(xxx) = 3 Then EXT = xxx(3)
* If UBound(xxx) = 2 Then EXT = xxx(2)
* If UBound(xxx) = 1 Then EXT = xxx(1)
* If UBound(xxx) = 0 Then EXT = xxx(0)
* If UBound(xxx) = 0 Then EXT = ""
* If Left(EXT, 1) = "." Then EXT = Right(EXT, Len(EXT) - 1)
* Select Case UCase(part)
* Case "EXT": FileExt = EXT
* Case Else: Filename = "part '" & part & "' not valid"
* End Select
* End Function


If you are ready to use FileSystemObject then it is very
simple.FilName is the path of file.

Use following code:

Dim Ext as String ' For Holding Extension Name
Set FSO=CreateObject("Scripting.FileSystemObject")
Ext=FSO.GetExtensionName("FilName")



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to insert filename in header without file extension Andrew King Excel Discussion (Misc queries) 4 January 12th 10 09:48 PM
Get Filename without the extension Larry S Excel Programming 9 October 23rd 08 08:44 AM
Excel 2007 File Extension Error [email protected] Excel Discussion (Misc queries) 8 January 31st 08 06:12 PM
Excel 2007 Ribbon file extension Dennis Excel Discussion (Misc queries) 6 November 26th 07 06:01 PM
How can I save a file in Excel 2007 beta 2 as a .dbf extension? Darren Crewes Excel Discussion (Misc queries) 1 June 3rd 06 11:31 PM


All times are GMT +1. The time now is 05:39 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"