Know a file extension vector?

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.
Configuration: Windows XP Firefox 3.0.9

15 answers

  • Votes
  • Date
  • Descending date
  1. 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
    1. Contributor
      How does the user give you the file path? via a dialog box or something else??

      In which language?? vb, php, c++?
      1. nana it’s in HTML :')
    2. 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.
      1. Contributor
        so everything after the '.'..

        ext=Right(ActiveWorkbook.FullName, 5)
        if left(ext,1)<> "." then ext=Right(ActiveWorkbook.FullName, 4)

        Tiny hiccup: if your Excel file is named "monfichier..xls" (2003), it will be treated as 2007, but otherwise it works
        1. yes it’s also another way of looking at it but you have to be careful because if the extension is longer than 5 characters ....
          Thanks for your answer :)
      2. 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 ^^.
        1. 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
          1. Moderator
            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 ????
            1.  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.
              1. Contributor
                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 :)
                1. Contributor
                  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 :)
                  1. Contributor
                    oops, désolé :)

                    Right(ActiveWorkbook.FullName, 3)
                    1. 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.
                  2. All responses are bad and dirty.
                    The official method is with the Shell API (Microsoft code)
                    1. Contributor
                      and so, a bit of code so you don’t die, you ignoramus ??
                    2. Yes, that's clear. There's a topic, a question, so might as well answer to give a valid response...
                      I'm also interested in the solution with the shell.
                    3. System.IO.Path.GetExtension(fileName)