Home > OS >  How do I get my code to recognise subject lines with # (hash)?
How do I get my code to recognise subject lines with # (hash)?

Time:09-17

I have code wherein I am able to input the subject of an email I have received within my Lotus Notes inbox and an email address. Like this:

Forward_Email "Subject text*", "[email protected]"

Then the code looks through my inbox for the Subject text, if it finds an email with the subject that is a match (in this case if it finds an email with the subject line Subject text) it will then forward that email to the email address specified.

However the issue is, when the subject line contains the character # (hash) the code is no longer able to find that subject line in the inbox and drops the vbCrLf & findSubjectLike & vbCrLf & "not found in Inbox" MsgBox as a response.

How do I get it to recognise subject lines with # (hash)?

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

Any help would be much appreciated.

CodePudding user response:

The issue is

If LCase(thisSubject) Like LCase(findSubjectLike) Then

The Like Operator accepts pattern matching and the # is a wildcard and stands for Any single digit (0-9).

So in If "#Subject text" Like "#Subject text" Then it looks for a numeric digit instead of #. So you need to replace # by [#] in findSubjectLike so it maches [charlist] Any single character in charlist.

LCase(Replace$(findSubjectLike, "#", "[#]"))

Note that you might also run into issues with ? and * as well as [ and ] characters in the subject as they are special characters in the Like operator.

If you are not using the pattern matching of the Like operator …

… and try to find exact matches then you can just switch form Like to =

If LCase(thisSubject) = LCase(findSubjectLike) Then

… and try to find out if findSubjectLike is part of thisSubject then you can use the InStr function

If InStr(thisSubject, findSubjectLike) > 0 Then
  • Related