Attribute VB_Name = "modPublic" Option Explicit ' Enumeration constants used with ParsePath procedure. Public Enum opgParsePath FILE_ONLY PATH_ONLY DRIVE_ONLY FILEEXT_ONLY FILE_ONLY_NO_EXT End Enum ' Enumeration constants for items that this add-in treats ' as entities. Public Enum opgComponentType INDIVIDUAL_PROC = 0 STANDARD_MODULE = 1 CLASS_MODULE = 2 VB_PROJECT = 3 End Enum ' Enumeration constants mapping to the code types available ' in the Code Librarian add-in. Public Enum opgCodeLibCodeType CODE_SNIPPET = 0 CODE_FUNCTION = 1 CODE_MODULE = 2 CODE_CLASS_MODULE = 3 CODE_PROPERTY = 4 End Enum ' Enumerated constants indicating whether a node refers to a ' module or a procedure. Public Enum opgNodeRefersTo OBJ_MODULE = 0 OBJ_PROC = 1 End Enum Function ParseProcType(strProcText As String) As opgCodeLibCodeType ' Parses procedure text to determine what type it is. Dim strFirstLine As String Dim astrFirstLine() As String ' Determine type of procedure. ' VB constant doesn't distinguish between Sub and ' Function procedures, so we parse the first line ' ourselves here. strFirstLine = Left(strProcText, _ InStr(strProcText, vbCrLf)) ' Split first line into array of words. astrFirstLine() = Split(strFirstLine, , , vbTextCompare) ' First or second word will contain function type. If astrFirstLine(0) = "Sub" Or astrFirstLine(1) = "Sub" Then ParseProcType = opgCodeLibCodeType.CODE_SNIPPET ElseIf astrFirstLine(0) = "Function" Or astrFirstLine(1) = _ "Function" Then ParseProcType = opgCodeLibCodeType.CODE_FUNCTION ElseIf astrFirstLine(0) = "Property" Or astrFirstLine(1) = _ "Property" Then ParseProcType = opgCodeLibCodeType.CODE_PROPERTY Else ParseProcType = -1 End If End Function Function ParsePath(strPath As String, _ lngPart As opgParsePath) As String ' This procedure takes a file path and returns ' either the path portion or the file portion, ' 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 opgParsePath.FILE_ONLY_NO_EXT If blnIncludesFile Then strPart = Right$(strPath, Len(strPath) - lngPos) strPart = Left$(strPart, Len(strPart) - 4) Else strPart = "" End If Case Else strPart = "" End Select End If ParsePath = strPart ParsePath_End: Exit Function End Function Function TrimSpace(strInput As String) As String ' This procedure trims extra space from any part of ' a string. Dim astrInput() As String Dim astrText() As String Dim strElement As String Dim lngCount As Long Dim lngIncr As Long ' Split passed-in string. astrInput = Split(strInput) ' Resize second array to be same size. ReDim astrText(UBound(astrInput)) ' Initialize counter variable for second array. lngIncr = LBound(astrInput) ' Loop through split array, looking for ' non-zero-length strings. For lngCount = LBound(astrInput) To UBound(astrInput) strElement = astrInput(lngCount) If Len(strElement) > 0 Then ' Store in second array. astrText(lngIncr) = strElement lngIncr = lngIncr + 1 End If Next ' Resize new array. If lngIncr > 0 Then ReDim Preserve astrText(LBound(astrText) To lngIncr - 1) End If ' Join new array to return string. TrimSpace = Join(astrText) End Function Function BinarySearch(varArray As Variant, _ varSearch As Variant) As Variant ' This function searches an array for an item and returns ' the index of the first matching element, if one is found. Dim lngUpper As Long Dim lngLower As Long Dim lngMid As Long ' Check whether variant is an array. If IsArray(varArray) Then ' If so, sort it. QuickSortArray varArray ' Store upper and lower bounds. lngUpper = UBound(varArray) lngLower = LBound(varArray) ' Perform loop while upper and ' lower bounds are not equal. Do While lngLower < lngUpper ' Find middle element. lngMid = (lngUpper + lngLower) \ 2 ' Determine whether sought element falls ' into this range. ' Resize upper and lower bounds. If varSearch > varArray(lngMid) Then lngLower = lngMid + 1 Else lngUpper = lngMid End If Loop ' Return index of element if it is found. ' Otherwise return -1. If varArray(lngLower) = varSearch Then BinarySearch = lngLower Else BinarySearch = -1 End If Else ' Return Null if an array wasn't passed in. BinarySearch = Null End If End Function Function QuickSortArray(varArray As Variant, _ Optional lngFirst As Long = -1, _ Optional lngLast As Long = -1) As Variant ' QuickSort algorithm used to sort the items ' in the varArray array. Dim lngLow As Long Dim lngHigh As Long Dim lngMiddle As Long Dim varTempVal As Variant Dim varTestVal As Variant If lngFirst = -1 Then lngFirst = LBound(varArray) If lngLast = -1 Then lngLast = UBound(varArray) If lngFirst < lngLast Then lngMiddle = (lngFirst + lngLast) / 2 varTestVal = varArray(lngMiddle) lngLow = lngFirst lngHigh = lngLast Do Do While varArray(lngLow) < varTestVal lngLow = lngLow + 1 Loop Do While varArray(lngHigh) > varTestVal lngHigh = lngHigh - 1 Loop If (lngLow <= lngHigh) Then varTempVal = varArray(lngLow) varArray(lngLow) = varArray(lngHigh) varArray(lngHigh) = varTempVal lngLow = lngLow + 1 lngHigh = lngHigh - 1 End If Loop While (lngLow <= lngHigh) If lngFirst < lngHigh Then QuickSortArray varArray, lngFirst, lngHigh If lngLow < lngLast Then QuickSortArray varArray, lngLow, lngLast End If End Function Public Function IsPublic(strProcName As String, _ lngProcType As Long, _ vbcComp As VBIDE.VBComponent) As Boolean ' Determines whether a procedure is public. Dim lngBodyLine As Long Dim strBodyLine As String Dim astrBodyLine() As String With vbcComp.CodeModule ' Get line number for procedure's body line. lngBodyLine = .ProcBodyLine(strProcName, lngProcType) ' Get text of body line. strBodyLine = .Lines(lngBodyLine, 1) ' Split body line into an array. astrBodyLine = Split(strBodyLine) ' Examine first element to determine whether procedure is ' Public, Private, or Friend. Select Case astrBodyLine(0) Case "Public", "Friend" IsPublic = True Case "Private" IsPublic = False Case Else ' If scope is not specified, procedures are public ' by default. IsPublic = True End Select End With End Function Function FilterExactMatch(astrItems() As String, _ strSearch As String, _ lngCompare As VbCompareMethod) As String() ' This function searches a string array for elements ' that exactly match the search string. Dim astrFilter() As String Dim astrTemp() As String Dim astrEmpty() As String Dim lngUpper As Long Dim lngLower As Long Dim lngIndex As Long Dim lngCount As Long ' Filter array for search string. astrFilter = Filter(astrItems, strSearch, True, lngCompare) ' Store upper and lower bounds of resulting array. lngUpper = UBound(astrFilter) lngLower = LBound(astrFilter) ' If Filter function returns an array with an upper bound of ' -1 and a lower bound of 0, then there are no matching items. If lngUpper < lngLower Then ' Return array containing no items and exit. FilterExactMatch = astrFilter GoTo FilterExactMatch_End End If ' Resize temporary array to be same size. ReDim astrTemp(lngLower To lngUpper) ' Loop through each element in filtered array. For lngIndex = lngLower To lngUpper ' Check that element matches search string exactly. If astrFilter(lngIndex) = strSearch Then ' Store elements that match exactly in another array. astrTemp(lngCount) = strSearch lngCount = lngCount + 1 End If Next lngIndex If lngCount > 0 Then ' Resize array containing exact matches. ReDim Preserve astrTemp(lngLower To lngCount - 1) ' Return array containing exact matches. FilterExactMatch = astrTemp Else ' This is a hack, but unfortunately there's no way to specify ' an array with an upper bound of -1 and a lower bound of 0. ' This may return an incorrect result if there's a null ' character in the string. ' Generate empty array. astrEmpty = Filter(astrItems, vbNullChar) FilterExactMatch = astrEmpty End If FilterExactMatch_End: Exit Function End Function