问题
I have a code for Outlook that processes any incoming item and if given criteria are passed a new appointment is to be created in Outlook calendar from mail items only.
The code does not differentiate between mail item and meeting request item. This results in the system creating a new meeting in year 1899 from the meeting item instead ignoring this.
Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
On Error Resume Next
Set ns = Application.Session
arr = Split(EntryIDCollection, ",")
For i = 0 To UBound(arr)
Set itm = ns.GetItemFromID(arr(i))
If (itm.Class = olMail) Then
Set objMail = itm
If objMail.Subject = "Approved" And objMail.Sender = "x@mail.com" Then
Set Reg1 = New RegExp
With Reg1
.Pattern = "([0-9]{2})(.)([0-9]{2})(.)([0-9]{4})(\s)(\W)(\s)([0-9]{2})(.)([0-9]{2})(.)([0-9]{4})"
.Global = True
End With
If Reg1.test(objMail.Body) Then
Set M1 = Reg1.Execute(objMail.Body)
For Each m In M1
Set objAppt = Application.CreateItem(olAppointmentItem)
Set objInsp = objAppt.GetInspector
Set objDoc = objInsp.WordEditor
Set objSel = objDoc.Windows(1).Selection
Next
End if
.....
End Sub
回答1:
Based on the OP's comment "Run-time error '13', type mismatch for the itm variable" and solution to the development problem, the answer to the question is likely to fix the misuse of On Error Resume Next and to set up the editor to generate Option Explicit on every module.
The programming change should then be:
Dim itm As Object
Dim objMail As MailItem
回答2:
You need to check the Class
property of the incoming item in the following manner:
If (itm.Class = olMail) Then
Set objMail = itm
...
End If
If (itm.Class = olMeetingRequest) Then
Set objMeeting = itm
...
End If
回答3:
Solved with help of another thread.
Dim itm As Object
Dim oMail As MailItem
If TypeName(itm) = "MailItem" Then
Set oMail = itm
....
End if
来源:https://stackoverflow.com/questions/45555331/code-ignores-incoming-meeting-request