Question : Email activation failed.

Hi Experts,

I would like to request Experts Help. For some reason the attached script not activate outlook email even though I’ve the Outlook reference is active. Hope Experts can help me to rectify this problem. I’ve attached the workbook for your reference.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
Sub SendEmail()
  'Uses early binding
  'Requires a reference to the Outlook Object Library
  Dim OutlookApp As Outlook.Application
  Dim MItem As Outlook.MailItem
  Dim olAppointment As Outlook.AppointmentItem
  
  Dim cell As Range
  Dim Subj As String
  Dim EmailAddr As String
  Dim Adate As Date
  Dim Atime As String
  Dim Msg2 As String
  Dim SendStatus As String
  
      
  'Create Outlook object
  Set OutlookApp = New Outlook.Application
  
  'Loop through the rows
  For Each cell In Columns("H").Cells.SpecialCells(xlCellTypeConstants)
    If cell.Value Like "*@*" Then
    
      'Get the data
      SendStatus = cell.Offset(0, 1)
      If SendStatus <> "To be sent" Then GoTo NotThisOne
      Subj = cell.Offset(0, -1).Value
      EmailAddr = cell.Value
      Adate = cell.Offset(0, -6).Value
      Atime = cell.Offset(0, -5).Value
      FileLocation = "C:\ApptFiles\OutlookAppointment.ics"
      cell.Offset(0, 1).Value = "Sent"

      
    Dim olApp As Outlook.Application
    Dim olApt As AppointmentItem
    
    Set olApp = New Outlook.Application
    Set olApt = olApp.CreateItem(olAppointmentItem)
    Msg2 = "New Task"
    
    With olApt
        .Start = Adate
        .End = .Start + TimeValue("00:30:00")
        .Subject = Msg2
        .Location = " "
        .Body = Msg
        .BusyStatus = olBusy
        .ReminderMinutesBeforeStart = 2880
        .ReminderSet = True
        '.Display
        
        'Save the iCalendar file in a known folder
        .SaveAs "C:\ApptFiles\OutlookAppointment.ics", olICal
        
        'Use Close to retain the new appointment within the Outlook Calendar, or Delete to delete it.
        'Both options keep the just-created .ics file
        
        '.Close False
        .Delete
    
    End With

      Set MItem = OutlookApp.CreateItem(olMailItem)
      With MItem
        .To = EmailAddr
        .Subject = Subj
        .Body = Msg
        .Attachments.Add (FileLocation)
        .Send
        '.Save 'to Drafts folder
      End With
NotThisOne:
    End If
  Next
  Set OutlookApp = Nothing
  Set olApt = Nothing
  Set olApp = Nothing

End Sub
Attachments:
 
 

Answer : Email activation failed.

HAve set the appointment for 7 days after run time and other defaults as I see fit - all can of course be changed.

Chris
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
Sub SendEmail()
Dim olkApp As Object
Dim MItem As Object
Dim olkAppointment As Object
  
Dim cell As Range
Dim Subj As String
Dim EmailAddr As String
Dim Adate As Date
Dim Atime As String
Dim Msg As String
Dim Msg2 As String
Dim SendStatus As String
Dim FileLocation As String
Dim strApptBody As String

Const olAppointmentItem As Integer = 1
Const olBusy As Integer = 2
Const olICal As Integer = 8
Const olMailItem As Integer = 0
      
  'Loop through the rows
  For Each cell In Columns("H").Cells.SpecialCells(xlCellTypeConstants)
    If cell.Value Like "*@*" Then
    
      'Get the data
      SendStatus = cell.Offset(0, 1)
      If SendStatus <> "To be sent" Then GoTo NotThisOne
      Subj = cell.Offset(0, -1).Value
      EmailAddr = cell.Value
      Adate = cell.Offset(0, -6).Value
      Atime = cell.Offset(0, -5).Value
      FileLocation = "C:\ApptFiles\OutlookAppointment.ics"
'FileLocation = Environ("temp") & "\" & "OutlookAppointment.ics"
    cell.Offset(0, 1).Value = "Sent"
   
    Set olkApp = CreateObject("outlook.application")
    Set olkAppointment = olkApp.CreateItem(olAppointmentItem)
    Msg2 = "New Task"
    
    With olkAppointment
        .Start = Adate
        .End = .Start + TimeValue("00:30:00")
        .Subject = Msg2
        .Location = " "
        .Body = Msg
        .BusyStatus = olBusy
        .ReminderMinutesBeforeStart = 2880
        .ReminderSet = True
        '.Display
        
        'Save the iCalendar file in a known folder
        .SaveAs FileLocation, olICal
        
        'Use Close to retain the new appointment within the Outlook Calendar, or Delete to delete it.
        'Both options keep the just-created .ics file
        
        '.Close False
        .Delete
    
    End With

      Set MItem = olkApp.CreateItem(olMailItem)
      With MItem
        .To = EmailAddr
        .Subject = Subj
        .Body = Msg
        .Attachments.Add (FileLocation)
        .Send
        '.Save 'to Drafts folder
      End With
      strApptBody = strApptBody & "Item: " & Format(cell.Offset(0, -6).Value + cell.Offset(0, -5).Value, "dd mmm yyyy hh:mm") & " | " & cell.Offset(0, -1).Value & vbCrLf
NotThisOne:
    End If
  Next
  If strApptBody <> "" Then
        With olkApp.CreateItem(olAppointmentItem)
            .Start = DateAdd("d", 7, Now())
            .End = .Start + TimeValue("00:30:00")
            .Subject = "Event Record"
            .Location = " "
            .Body = strApptBody
            .BusyStatus = olBusy
            .ReminderMinutesBeforeStart = 2880
            .ReminderSet = True
            .Save
            .Close False
    End With
  End If
  Set olkApp = Nothing
  Set olkAppointment = Nothing
  Set olkApp = Nothing

End Sub
Random Solutions  
 
programming4us programming4us