如何让Lotus Notes邮件转发VB代码识别包含#(井号)的主题行?
问题根因
你的代码匹配失败的核心原因是VBA的Like运算符中,#是系统预留的通配符,默认代表匹配任意单个数字字符,不会被识别为普通的井号字面量,因此主题中包含#的邮件无法正常匹配。
解决方法
根据你的使用需求可以选择以下两种修改方案:
方案1:保留原有通配符能力(推荐,兼容你现有的*模糊匹配用法)
仅对井号做转义处理,其余通配符(*、?等)的功能保持不变,修改Find_Document函数即可:
Private Function Find_Document(NView As Object, findSubjectLike As String) As Object Dim NThisDoc As Object Dim thisSubject As String Dim escapedPattern As String Set Find_Document = Nothing ' 转义#为[#],让Like将其识别为普通井号字符 escapedPattern = Replace(LCase(findSubjectLike), "#", "[#]") Set NThisDoc = NView.GetFirstDocument While Not NThisDoc Is Nothing And Find_Document Is Nothing thisSubject = NThisDoc.GetItemValue("Subject")(0) If LCase(thisSubject) Like escapedPattern Then Set Find_Document = NThisDoc Set NThisDoc = NView.GetNextDocument(NThisDoc) Wend End Function
方案2:完全精确匹配(所有输入字符都按字面匹配,不启用通配符)
如果你不需要用到*模糊匹配等通配符功能,希望输入的所有字符都原样匹配,可以新增一个通配符转义函数,再修改匹配逻辑:
首先新增辅助转义函数:
Private Function EscapeLikePattern(pattern As String) As String ' 转义VBA Like所有保留通配符,转为字面量匹配 EscapeLikePattern = Replace(pattern, "[", "[[]") EscapeLikePattern = Replace(EscapeLikePattern, "]", "[]]") EscapeLikePattern = Replace(EscapeLikePattern, "?", "[?]") EscapeLikePattern = Replace(EscapeLikePattern, "#", "[#]") EscapeLikePattern = Replace(EscapeLikePattern, "*", "[*]") End Function
再修改Find_Document函数的匹配逻辑:
Private Function Find_Document(NView As Object, findSubjectLike As String) As Object Dim NThisDoc As Object Dim thisSubject As String Dim escapedPattern As String Set Find_Document = Nothing escapedPattern = LCase(EscapeLikePattern(findSubjectLike)) Set NThisDoc = NView.GetFirstDocument While Not NThisDoc Is Nothing And Find_Document Is Nothing thisSubject = NThisDoc.GetItemValue("Subject")(0) If LCase(thisSubject) Like escapedPattern Then Set Find_Document = NThisDoc Set NThisDoc = NView.GetNextDocument(NThisDoc) Wend End Function
效果验证
修改完成后,类似Forward_Email "订单#2024*", "test@address.com"的调用就可以正常匹配到主题包含#的邮件,不会再弹出未找到的提示。
内容的提问来源于stack exchange,提问作者Drawleeh
相关产品推荐
相关产品推荐

