问题
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