Returning Portions of a String
To work with part of a string's contents, you must parse the string. You can use the InStr or InStrRev function to find the position at which to begin parsing the string. When you've located that position, you can use the Left, Right, and Mid functions to do the job. The Left and Right functions return a specified number of characters from either the left or right portion of the string. The Mid function is the most flexible of the parsing functions — you can specify a starting point anywhere within the string, followed by the number of characters you want to return.
Note Some of the Microsoft® Visual Basic® for Applications (VBA) string functions come in two varieties, one that returns a string, and one that returns a string-type Variant value. The names of the functions that return a string include a dollar sign ("$"); for example, Chr$, Format$, LCase$, Left$, LTrim$, Mid$, Right$, RTrim$, Space$, Trim$, and UCase$. The functions that return a string-type Variant value have no dollar sign; for example, Chr, Format, LCase, Left, LTrim, Mid, Right, RTrim, Space, Trim, and UCase. The string-returning functions are faster; however, you'll get an error if you call them with a value that is Null. The functions that return a string-type Variant value handle Null values without an error. Code examples in this section use the string-returning functions where appropriate.
The following procedure parses a file path and returns one of the following portions: the path (everything but the file name), the file name, the drive letter, or the file extension. You specify which part of the string you want to return by passing a constant to the lngPart argument. The lngPart argument is defined as type opgParsePath, which contains custom enumerated constants declared in the modPublicDefs module in VBA.mdb.
Note that this procedure uses the InStrRev function to find the last path separator, or backslash (\), in the string. If you used the InStr function, you'd have to write a loop to make sure that you'd found the last one. With the InStrRev function, you know that the first backslash you find is actually the last one in the string, and the characters to the right of it must be the file name.
Function ParsePath(strPath As String, _
lngPart As opgParsePath) As String
' This procedure takes a file path and returns
' the path (everything but the file name), the drive letter,
' or the file extension,
' depending on which constant was passed in.
Dim lngPos As Long
Dim strPart As String
Dim blnIncludesFile As Boolean
' Check that this is a file path.
' Find the last path separator.
lngPos = InStrRev(strPath, "\")
' Determine whether portion of string after last backslash
' contains a period.
blnIncludesFile = InStrRev(strPath, ".") > lngPos
If lngPos > 0 Then
Select Case lngPart
' Return file name.
Case opgParsePath.FILE_ONLY
If blnIncludesFile Then
strPart = Right$(strPath, Len(strPath) - lngPos)
Else
strPart = ""
End If
' Return path.
Case opgParsePath.PATH_ONLY
If blnIncludesFile Then
strPart = Left$(strPath, lngPos)
Else
strPart = strPath
End If
' Return drive.
Case opgParsePath.DRIVE_ONLY
strPart = Left$(strPath, 3)
' Return file extension.
Case opgParsePath.FILEEXT_ONLY
If blnIncludesFile Then
' Take three characters after period.
strPart = Mid(strPath, InStrRev(strPath, ".") + 1, 3)
Else
strPart = ""
End If
Case Else
strPart = ""
End Select
End If
ParsePath = strPart
ParsePath_End:
Exit Function
End Function
Calling this function as follows from the Immediate window returns "Test.txt":
? ParsePath("C:\Temp\Test.txt", opgParsePath.FILE_ONLY)
See Also
Getting the Most Out of Visual Basic for Applications | Working with Strings | Comparing Strings | Calculating String Length | Searching a String | Working with Strings as Arrays | Replacing Text Within a String | Converting Strings | Working with String Variables