如何识别带有#(哈希)的主题行?
How to recognise subject lines with # (hash)?
我有代码可以输入在 Lotus Notes 中收到的电子邮件的主题和电子邮件地址。
Forward_Email "Subject text*", "email1@address.com"
然后代码会在我的收件箱中查找 Subject text
,如果找到匹配项,它将将该电子邮件转发到指定的电子邮件地址。
当主题行包含字符 #
(散列)时,代码无法在收件箱中找到该主题行并丢弃 vbCrLf & findSubjectLike & vbCrLf & "not found in Inbox"
MsgBox 作为响应。
如何让它识别带有 #
(散列)的主题行?
Public Sub Forward_Email(findSubjectLike As String, forwardToEmailAddresses As String)
Dim NSession As Object
Dim NMailDb As Object
Dim NViewObj As Variant
Dim NInboxView As Object
Dim NDocument As Object
Dim NUIWorkspace As Object
Dim NUIDocument As Object
Dim NFwdUIDocument As Object
Set NSession = CreateObject("Notes.NotesSession")
Set NUIWorkspace = CreateObject("Notes.NotesUIWorkspace")
Set NMailDb = NSession.CurrentDatabase
For Each NViewObj In NMailDb.Views
If NViewObj.IsFolder And NViewObj.Name = "($Inbox)" Then
Set NInboxView = NViewObj
Exit For
End If
Next
Set NDocument = Find_Document(NInboxView, findSubjectLike)
If Not NDocument Is Nothing Then
Set NUIDocument = NUIWorkspace.EditDocument(False, NDocument)
NUIDocument.Forward
Set NFwdUIDocument = NUIWorkspace.CurrentDocument
NFwdUIDocument.GoToField "To"
NFwdUIDocument.InsertText forwardToEmailAddresses
NFwdUIDocument.GoToField "Body"
NFwdUIDocument.InsertText "This email was forwarded at " & Now
NFwdUIDocument.InsertText vbLf
NFwdUIDocument.Send
NFwdUIDocument.Close
Do
Set NUIDocument = NUIWorkspace.CurrentDocument
Sleep 100
DoEvents
Loop While NUIDocument Is Nothing
NUIDocument.Close
Else
MsgBox vbCrLf & findSubjectLike & vbCrLf & "not found in Inbox"
End If
Set NUIDocument = Nothing
Set NFwdUIDocument = Nothing
Set NDocument = Nothing
Set NMailDb = Nothing
Set NUIWorkspace = Nothing
Set NSession = Nothing
End Sub
Private Function Find_Document(NView As Object, findSubjectLike As String) As Object
Dim NThisDoc As Object
Dim thisSubject As String
Set Find_Document = Nothing
Set NThisDoc = NView.GetFirstDocument
While Not NThisDoc Is Nothing And Find_Document Is Nothing
thisSubject = NThisDoc.GetItemValue("Subject")(0)
If LCase(thisSubject) Like LCase(findSubjectLike) Then Set Find_Document = NThisDoc
Set NThisDoc = NView.GetNextDocument(NThisDoc)
Wend
End Function
问题是
If LCase(thisSubject) Like LCase(findSubjectLike) Then
Like Operator接受模式匹配,#
是一个通配符,代表任何一位数字(0-9)。
因此在 If "#Subject text" Like "#Subject text" Then
中它查找数字而不是 #
。所以你需要用 [#]
替换 findSubjectLike
中的 #
以便它匹配 [charlist]
charlist 中的任何单个字符。
LCase(Replace$(findSubjectLike, "#", "[#]"))
请注意,您可能还会 运行 对主题中的 ?
和 *
以及 [
和 ]
字符存在问题Like
运算符中的特殊字符。
如果您没有使用 Like
运算符的模式匹配……
… 并尝试找到 完全 匹配然后你可以将形式 Like
切换为 =
If LCase(thisSubject) = LCase(findSubjectLike) Then
... 并尝试找出 findSubjectLike
是否是 thisSubject
的一部分,然后您可以使用 InStr function
If InStr(thisSubject, findSubjectLike) > 0 Then
我有代码可以输入在 Lotus Notes 中收到的电子邮件的主题和电子邮件地址。
Forward_Email "Subject text*", "email1@address.com"
然后代码会在我的收件箱中查找 Subject text
,如果找到匹配项,它将将该电子邮件转发到指定的电子邮件地址。
当主题行包含字符 #
(散列)时,代码无法在收件箱中找到该主题行并丢弃 vbCrLf & findSubjectLike & vbCrLf & "not found in Inbox"
MsgBox 作为响应。
如何让它识别带有 #
(散列)的主题行?
Public Sub Forward_Email(findSubjectLike As String, forwardToEmailAddresses As String)
Dim NSession As Object
Dim NMailDb As Object
Dim NViewObj As Variant
Dim NInboxView As Object
Dim NDocument As Object
Dim NUIWorkspace As Object
Dim NUIDocument As Object
Dim NFwdUIDocument As Object
Set NSession = CreateObject("Notes.NotesSession")
Set NUIWorkspace = CreateObject("Notes.NotesUIWorkspace")
Set NMailDb = NSession.CurrentDatabase
For Each NViewObj In NMailDb.Views
If NViewObj.IsFolder And NViewObj.Name = "($Inbox)" Then
Set NInboxView = NViewObj
Exit For
End If
Next
Set NDocument = Find_Document(NInboxView, findSubjectLike)
If Not NDocument Is Nothing Then
Set NUIDocument = NUIWorkspace.EditDocument(False, NDocument)
NUIDocument.Forward
Set NFwdUIDocument = NUIWorkspace.CurrentDocument
NFwdUIDocument.GoToField "To"
NFwdUIDocument.InsertText forwardToEmailAddresses
NFwdUIDocument.GoToField "Body"
NFwdUIDocument.InsertText "This email was forwarded at " & Now
NFwdUIDocument.InsertText vbLf
NFwdUIDocument.Send
NFwdUIDocument.Close
Do
Set NUIDocument = NUIWorkspace.CurrentDocument
Sleep 100
DoEvents
Loop While NUIDocument Is Nothing
NUIDocument.Close
Else
MsgBox vbCrLf & findSubjectLike & vbCrLf & "not found in Inbox"
End If
Set NUIDocument = Nothing
Set NFwdUIDocument = Nothing
Set NDocument = Nothing
Set NMailDb = Nothing
Set NUIWorkspace = Nothing
Set NSession = Nothing
End Sub
Private Function Find_Document(NView As Object, findSubjectLike As String) As Object
Dim NThisDoc As Object
Dim thisSubject As String
Set Find_Document = Nothing
Set NThisDoc = NView.GetFirstDocument
While Not NThisDoc Is Nothing And Find_Document Is Nothing
thisSubject = NThisDoc.GetItemValue("Subject")(0)
If LCase(thisSubject) Like LCase(findSubjectLike) Then Set Find_Document = NThisDoc
Set NThisDoc = NView.GetNextDocument(NThisDoc)
Wend
End Function
问题是
If LCase(thisSubject) Like LCase(findSubjectLike) Then
Like Operator接受模式匹配,#
是一个通配符,代表任何一位数字(0-9)。
因此在 If "#Subject text" Like "#Subject text" Then
中它查找数字而不是 #
。所以你需要用 [#]
替换 findSubjectLike
中的 #
以便它匹配 [charlist]
charlist 中的任何单个字符。
LCase(Replace$(findSubjectLike, "#", "[#]"))
请注意,您可能还会 运行 对主题中的 ?
和 *
以及 [
和 ]
字符存在问题Like
运算符中的特殊字符。
如果您没有使用 Like
运算符的模式匹配……
… 并尝试找到 完全 匹配然后你可以将形式 Like
切换为 =
If LCase(thisSubject) = LCase(findSubjectLike) Then
... 并尝试找出 findSubjectLike
是否是 thisSubject
的一部分,然后您可以使用 InStr function
If InStr(thisSubject, findSubjectLike) > 0 Then