Know a file extension vector?
matthoffman
Posted messages
404
Registration date
Status
Member
Last intervention
-
atchalak -
atchalak -
Hello,
I would like to know if there is a function (if there is one) that returns the extension (or the type) of a file.
If you don't know and you know a function that returns the version of Microsoft Office, that's just as well :).
Thanks in advance.
I would like to know if there is a function (if there is one) that returns the extension (or the type) of a file.
If you don't know and you know a function that returns the version of Microsoft Office, that's just as well :).
Thanks in advance.
Configuration: Windows XP Firefox 3.0.9
15 answers
Yeah well thanks for your help :)
your method is 'porc' in the sense that you play with fire ;) but it seems the most suitable for my problem.
Provided I create a file .blablabla, it will mess up during processing because it will think the extension is "abla" instead of "blablabla" lol
your method is 'porc' in the sense that you play with fire ;) but it seems the most suitable for my problem.
Provided I create a file .blablabla, it will mess up during processing because it will think the extension is "abla" instead of "blablabla" lol
But the post title is VB at the same time, so the language is VB ;)
And I get the file path with the ActiveDocument.Path function .
In fact I’m writing a macro in an Excel (or Word) file and so there you go I retrieve the info from the open document.
And I get the file path with the ActiveDocument.Path function .
In fact I’m writing a macro in an Excel (or Word) file and so there you go I retrieve the info from the open document.
I just thought of a crude method to retrieve the extension.
We increment a counter until we find a "." in the filename starting from the right.
And the extension corresponds to the string on the right of length equal to the counter.
But as I just said, it's crude and way too long for what it is ^^.
We increment a counter until we find a "." in the filename starting from the right.
And the extension corresponds to the string on the right of length equal to the counter.
But as I just said, it's crude and way too long for what it is ^^.
Come on... I'm nice
Private Function FileExtension(NomFichier As String) As String
Dim DotPos As Integer
If InStr(1, NomFichier, ".") = 0 Then
FileExtension = False
Else
While Left(Right(NomFichier, DotPos), 1) <> "."
DotPos = DotPos + 1
Wend
FileExtension = Right(NomFichier, DotPos - 1)
End If
End Function
Hello,
I’m taking advantage of the revival of this topic to propose a variant solution:
Dim Extension As String Extension = IIf(Mid(StrReverse(ThisWorkbook.Name), 4, 1) = ".", Right(ThisWorkbook.Name, 3), Right(ThisWorkbook.Name, 4))-- ???? Cordially, Franck ????
Dim myFileName As String, fileExtension As String fileExtension = Right(myFileName, InStrRev(myFileName, "."))
Instr returns the first occurrence of the character passed as an argument, InStrRev allows starting from the right; as soon as we reach the first ".", we have only the file extension on the right. This method also handles files that have one or more "." inside.
ah ben sinon, faut fare le contraire, et la y a pas d exeption :
ext=Right(ActiveWorkbook.FullName, 4)
if left(ext,1)<>"." then ext=Right(ActiveWorkbook.FullName, 5)
:) :p
j suis vraiment nul quand j m y met :)
ext=Right(ActiveWorkbook.FullName, 4)
if left(ext,1)<>"." then ext=Right(ActiveWorkbook.FullName, 5)
:) :p
j suis vraiment nul quand j m y met :)
else, you can do something like:
ext=Right(ActiveWorkbook.FullName, 4) '2003
if left(ext,1)<> "." then
ext=Right(ActiveWorkbook.FullName, 5) '2007
if left(ext,1)<> "." then
ext=""
msgbox "extension error"
end if
good enough.. it's junk, but it handles all your exceptions :)
ext=Right(ActiveWorkbook.FullName, 4) '2003
if left(ext,1)<> "." then
ext=Right(ActiveWorkbook.FullName, 5) '2007
if left(ext,1)<> "." then
ext=""
msgbox "extension error"
end if
good enough.. it's junk, but it handles all your exceptions :)
Hmmm if I’m not mistaken, your function will return the last 3 letters of the full name, which works very well for Office 2003 but not for 2007 because extensions have 4 letters (docx, xlsx, ...).
I would need a function that automatically retrieves the extension so that my macro can adapt to both 2003 and 2007 without any hassle and without having to modify the code if the version changes.
I would need a function that automatically retrieves the extension so that my macro can adapt to both 2003 and 2007 without any hassle and without having to modify the code if the version changes.