Search for a string in a subject

旧城冷巷雨未停 提交于 2019-12-11 09:40:04

问题


I am using VBA in MSOutlook to search for a string in the subject line and if it is present then prompt a warning.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim strSubject As String
    strSubject = Item.Subject
    If strSubject.Contains("ZAFTM") or Else strSubject.Contains ("BENSP")    Then
        Prompt$ = "operator, Can I send the mail?"
        If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Subject") = vbNo Then
            Cancel = True
        End If
    End If
End Sub

This gives me an error for invalid qualifier for strSubject.


回答1:


You are mixing VB.net syntax with VBA. Strings in VBA are not objects and have no methods.

Use Instr() or Like in VBA

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim strSubject As String
strSubject = Item.Subject
If strSubject Like "*ZAFTM*" or strSubject Like "*BENSP*" Then

    Prompt$ = "operator, Can I send the mail?"

    If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, _
                                           "Check for Subject") = vbNo Then
        Cancel = True
    End If

End If
End Sub


来源:https://stackoverflow.com/questions/32171589/search-for-a-string-in-a-subject

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!