Subject: RE: Finding a folder by name, quickest way?
Caveat: I don’t consider this function to have been adequately tested – I only just wrote it and did some tire-kicking. If it needs corrections please let me know.
Function GetFoldersLike(db As NotesDatabase, Byval strMatch As String, Byval options As Integer) As Variant
%rem
Args are a database, a wildcard match string in the format used by @Matches macro function,
and an option flag which may contain 1 to specify case-insensitive search. Return value
is variant array of NotesView objects – folders whose titles matched the search string.
If no folder matched, the return value is EMPTY.
Because of an internal implementation issue, this routine may fail with an error if the search
matches a folder that does not have any unique name or alias (among views and folders in
that database). If that happens, your alternative is to iterate through db.Views.
%end rem
Dim notecoll As NotesNoteCollection
Set notecoll = db.CreateNoteCollection(False)
notecoll.SelectFolders = True
If options And 1 Then
notecoll.SelectionFormula = { @Matches(@lowercase($Title); } & _
macroquote(Lcase(strMatch)) & {) }
Else
notecoll.SelectionFormula = { @Matches($Title; } & macroquote(strMatch) & {) }
End If
notecoll.BuildCollection
If notecoll.Count > 0 Then
Redim retval(0 To notecoll.count - 1) As NotesView
Dim docDes As NotesDocument ’ the folder design note as a document
Dim strNoteID As String
Dim intIndex As Integer, intAliasID As Integer
Dim folderTitles As Variant
Dim viewTemp As NotesView
strNoteID = notecoll.GetFirstNoteId
Do ’ for each noteID in the result
Set docDes = db.GetDocumentByID(strNoteID)
folderTitles = Split(docDes.GetItemValue(“$TITLE”)(0), {|})
' try each name or alias to locate the folder using GetView.
For intAliasID = Ubound(folderTitles) To 0 Step -1
Set viewTemp = db.GetView(folderTitles(intAliasID))
If Not (viewTemp Is Nothing) Then
If viewTemp.UniversalID = docDes.UniversalID Then Exit For
End If
' if we reach here the alias was not unique and we try other names.
Next
If intAliasID < 0 Then
Error 20340, {Folder name "} & Join(folderTitles, " | ") & _
{" is not sufficiently unique to locate the folder.}
End If
Set retval(intIndex) = viewTemp
intIndex = intIndex + 1
Delete docDes
strNoteID = notecoll.GetNextNoteId(strNoteID)
Loop Until strNoteID = “”
GetFoldersLike = retval
End If
End Function
Function macroquote(Byval x As String) As String
’ convert string to macro code quoted string; change all " to " and add quotes at start and end.
Dim fromArr(0 To 0) As String
Dim toArr(0 To 0) As String
Dim targetArr(0 To 0) As String
If Instr(x, {“}) Then
fromArr(0) = {”}
toArr(0) = {"}
targetArr(0) = x
Dim result As Variant
result = Replace(targetArr, fromArr, toArr)
macroquote = {“} & result(0) & {”}
Else
macroquote = {“} & x & {”}
End If
End Function