Results 1 to 18 of 18

Trouble creating outlook event from

Threaded View

  1. #16
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Trouble creating outlook event from

    Hi Nick

    Replace the Code with this...

    Option Explicit
    Sub AddOutLookTask()
       ' add a refrence to Microsoft outlook object library
       Dim appOutLook   As Outlook.Application
       Dim taskOutLook  As Outlook.AppointmentItem
       Dim myItem       As Outlook.AppointmentItem
       Dim myItems      As Outlook.Items
       Dim data         As Worksheet
       Dim subject      As String
       Dim body         As String
       Dim wen          As Date
       Dim i            As Long
       Dim olNs         As Namespace
       Dim durationarray() As String
       Dim Duration     As Double
       Dim reminder1()  As String
       Dim Reminder     As Double
       Dim myArray()    As Variant
       Dim msgString    As String
    
       ReDim myArray(0)
       Set appOutLook = CreateObject("Outlook.Application")
       Set olNs = appOutLook.GetNamespace("MAPI")
       Set myItems = olNs.GetDefaultFolder(olFolderCalendar).Items
    
       '   Set taskOutLook = appOutLook.CreateItem(olAppointmentItem)
       Set data = ThisWorkbook.Worksheets("Agenda")   ' change worksheetname as required
       For i = 2 To Application.CountA(Sheets("Agenda").Range("A:A"))   ' loop through each row starting at row 2  (loop uses column A as a count - ensure column A always contains a name
          subject = data.Cells(i, 1)
          body = "You have a meeting with " & subject
          wen = DateValue(data.Cells(i, 2)) + data.Cells(i, 3)
          If InStr(data.Cells(i, 4), "hr") Then   ' does duration contain 'hr'
             durationarray = Split(data.Cells(i, 4), " ")   ' get digits before first space
             Duration = Val(durationarray(0))
          Else
             durationarray = Split(data.Cells(i, 4), " ")
             Duration = Val(durationarray(0)) / 60   'if duration does not contain hr  (i.e. mins) then convert mins to decimal of an hour  (30 mins = 0.5)
          End If
          reminder1 = Split(data.Cells(i, 5), " ")   ' assume always days and get digits before first space
          Reminder = reminder1(0)
    
          '      ReDim myArray(0)
          Set myItem = myItems.Find("[Subject]=" & Chr(34) & subject & Chr(34))
    
          If Not myItem Is Nothing Then
             If myItem.subject = subject And _
                myItem.Duration / 60 = Duration And _
                myItem.Start = wen Then
    
                myArray(UBound(myArray)) = subject & " already acheduled"
                ReDim Preserve myArray(UBound(myArray) + 1)
                '            MsgBox subject & " Already Scheduled"
                GoTo Skip_Me
             End If
    
          Else
             Set taskOutLook = appOutLook.CreateItem(olAppointmentItem)
             With taskOutLook
                .subject = subject
                .body = body
                .ReminderSet = True
                .ReminderMinutesBeforeStart = Reminder * 24 * 60   ' reminder in hours converted to minutes
                .Duration = Duration * 60   ' duration converted to minutes
                .Start = wen   ' start date/time
                .Save
             End With
          End If
          Set myItem = myItems.FindNext
    Skip_Me:
       Next
       On Error Resume Next
       ReDim Preserve myArray(UBound(myArray) - 1)
       On Error GoTo 0
       msgString = Join(myArray, vbCr)
       If Not IsError(Application.Match("*", (myArray), 0)) Then
          MsgBox msgString
       End If
       Set appOutLook = Nothing
       Set taskOutLook = Nothing
       Set data = Nothing
       Set taskOutLook = Nothing
    End Sub
    Last edited by jaslake; 04-08-2016 at 10:52 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Trouble with the 'worksheet_selectionchange' event
    By fosters_ in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-08-2014, 05:05 AM
  2. [SOLVED] Having Trouble Modifying Excel Sheet With Outlook
    By bullo1854 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-26-2014, 10:31 AM
  3. [SOLVED] Trouble stopping an Application.OnTime event
    By phuz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-14-2012, 09:34 AM
  4. Trouble with Selection_Change Event
    By jman0707 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-13-2008, 01:28 PM
  5. Trouble triggering an event when UsedRange >=A1500?
    By Simon Lloyd in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-20-2006, 11:50 PM
  6. Trouble using UsedRange to trigger an event??
    By Simon Lloyd in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-19-2006, 10:29 PM
  7. Event trouble...
    By Ernst Guckel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-05-2005, 06:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1