Trim the text
This is specific to putting pathnames into a ListBox. You will see, in many
places in Word, that filenames get shortened to fit the available space, by
putting an ellipsis (...) into the middle of the path name. The following
code allows you to do the same.
You pass a path name to the PathName parameter, and the maximum length in
characters of the truncated path to the PathLength parameter.
If the original pathname is less than the maximum length, then it is
returned by the function. If it is shorter than the maximum length, then the
function takes out one or more of the folder names of the path and replaces
them with an ellipsis.
If the required length is very short, then the function will return just the
first element (usually the drive letter) and the filename. The code below requires Word 2000 or later
to run, as it uses the Split function, which is not available in Word 97.
Public Function EllipsisPathName(PathName
As String, PathLength As
Long)
Dim vPath As Variant
Dim iLastFolder As Long
Dim iCurrentLength As Long
Dim iUsedFolder As Long
Dim strEllipsePath As String
If PathLength >= Len(PathName) Then
EllipsePathName = PathName
Exit Function
End If
iCurrentLength = Len(PathName) + 4
vPath = Split(PathName, "\")
For iLastFolder = UBound(vPath) - 1 To
1 Step -1
iCurrentLength = iCurrentLength - Len(vPath(iLastFolder)) - 1
If iCurrentLength <= PathLength Then
For iUsedFolder = 0
To iLastFolder - 1
strEllipsePath = strEllipsePath & vPath(iUsedFolder) & "\"
Next iUsedFolder
strEllipsePath = strEllipsePath & "...\" & vPath(UBound(vPath))
EllipsisPathName = strEllipsePath
Exit Function
End If
Next iLastFolder
EllipsisPathName = vPath(0) & "\...\" & vPath(UBound(vPath))
End Function |