0

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    [The documentation](https://msdn.microsoft.com/VBA/Outlook-VBA/articles/create-an-appointment-as-a-meeting-on-the-calendar) suggests you should set the [MeetingRequest property](https://msdn.microsoft.com/VBA/Outlook-VBA/articles/appointmentitem-meetingstatus-property-outlook) - `objAppt.MeetingStatus = olMeeting` – stuartd Aug 07 '17 at 20:44
  • So what is the exact problem? That the check "If (itm.Class = olMail) Then" tells you it is a mail item while it is a meeting item? – Dmitry Streblechenko Aug 07 '17 at 21:21
  • After a meeting request is sent and received in mailbox expected result is that it is going to fail given conditions as only mails are to be processed, ie. no meeting is created. The actual result now is that a whole day event with empty subject line, empty location line and start time 30.12.1899 and end time 29.12.1899 is created. Also the event body includes part of the code that processes the mail – Vojtěch Jarolím Aug 08 '17 at 22:26
  • Remove On Error Resume Next so you can see errors. If there is an error update the question. – niton Aug 10 '17 at 12:06
  • Good idea. Run-time error '13', type mismatch for the itm variable – Vojtěch Jarolím Aug 10 '17 at 20:07

3 Answers3

0

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 
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • I created basic log and tested it. It works for incoming mails, writes class 43. But this issue is that the code does not recognize the class of olMeetingRequest. It completely ignores it. It does not write anything in the log. `If itm.Class = olMeetingRequest Then ws.Range("E" & lr) = itm.Class End If` – Vojtěch Jarolím Aug 08 '17 at 22:42
0

Solved with help of another thread.

Dim itm As Object
Dim oMail As MailItem

If TypeName(itm) = "MailItem" Then

   Set oMail = itm
   ....
End if 
0

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
niton
  • 8,771
  • 21
  • 32
  • 52