Friday, November 11, 2011

Get File Extension - Hàm lấy phần mở rộng của file (VBA)

This function returns the file extension when passed the full filepath . The function searches for the leading fullstop to determine to start of the file extension. Add the following code to a FileHandling.bas standard module to compile a File Utility function set:
Function GetExtName(ScanString as String) As String
       
'*******************************************************
'  Retrieves File Extension Name from full
'  directory path
' File Extension Only
'   
' Public
'  FullPath: 
'   Full Filepath incl. Filename
'    
' If GetExtName("c:\autoexec.bat")
'    
'*******************************************************
    
    Dim intPos as String
    Dim intPosSave as String
    
    If InStr(ScanString, ".") = 0 Then
        GetExtName = ""
        Exit Function
    End If
    
    intPos = 1
    Do
        intPos = InStr(intPos, ScanString, ".")
        If intPos = 0 Then
            Exit Do
        Else
            intPos = intPos + 1
            intPosSave = intPos - 1
        End If
    Loop

    GetExtName = Trim$(Mid$(ScanString, intPosSave + 1))

End Function

No comments:

Post a Comment