The previous solutions all suffer from assumptions that the file
extension is X characters long (e.g. 3 or 4), or is such-and-such
character string (e.g. "htm"). Those kinds of assumptions will cause
debugging later on when reality intrudes. ;-)
Here's a more generic way to do this that will work with file
extensions of any length, no length, or containing multiple periods
(hence the StrReverse). It's coded as a function so it can be called
where ever needed w/o rewriting the code. If you don't know how to
integrate the function into your code you can just take the code
inside the function (wow, that sounded weird!).
________________________________________________________________
Sub joe()
Dim sFilename As String ' original filename
sFilename = "Hello World"
MsgBox (sFilename & vbCrLf & StripExtension(sFilename))
sFilename = "Hello World.htm"
MsgBox (sFilename & vbCrLf & StripExtension(sFilename))
sFilename = "Hello World.html"
MsgBox (sFilename & vbCrLf & StripExtension(sFilename))
sFilename = "Hello World.Peace.doc"
MsgBox (sFilename & vbCrLf & StripExtension(sFilename))
End Sub
________________________________________________________________
Function StripExtension(Filename As String) As String
Dim sEmanelif As String ' reversed filename
Dim sFileNoExt As String ' filename w/o extension
If InStr(1, Filename, ".") = 0 Then
StripExtension = Filename
Else
' reverse Filename string to get extension at front
sEmanelif = StrReverse(Filename)
' take from 1 past period onward
sEmanelif = Mid(sEmanelif, InStr(sEmanelif, ".") + 1)
' reverse back to original and place in function result
StripExtension = StrReverse(sEmanelif)
End If
End Function