Consulting

Results 1 to 8 of 8

Thread: Create Outlook 2013 Calendar items without duplicates

  1. #1

    Create Outlook 2013 Calendar items without duplicates

    Good morning / afternoon,
    I'm trying to create a macro that will allow me to add calendar items to an Outlook 2013 calendar, but if it already exists, to ignore it.
    Basically, I want to have a list of important dates, and if I add to it at a later date, people can click a button to add all the ones they don't already have

    The code I currently have is :

    Sub AddAppointments()
        ' Create the Outlook session
        Set myOutlook = CreateObject("Outlook.Application")
        ' Start at row 2
        r = 2
        Do Until Trim(Cells(r, 1).Value) = ""
            ' Create the AppointmentItem
            Set myApt = myOutlook.createitem(1)
            ' Set the appointment properties
            myApt.Subject = Cells(r, 1).Value
            myApt.Location = Cells(r, 2).Value
            myApt.Start = Cells(r, 3).Value
            myApt.Duration = Cells(r, 4).Value
            ' If Busy Status is not specified, default to 2 (Busy)
            If Trim(Cells(r, 5).Value) = "" Then
                myApt.BusyStatus = 2
            Else
                myApt.BusyStatus = Cells(r, 5).Value
            End If
            If Cells(r, 6).Value > 0 Then
                myApt.ReminderSet = True
                myApt.ReminderMinutesBeforeStart = Cells(r, 6).Value
            Else
                myApt.ReminderSet = False
            End If
            myApt.Body = Cells(r, 7).Value
            myApt.Save
            r = r + 1
        Loop
    End Sub

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Might be better to post this in the Outlook forum or ask one of the mods to move it for you
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Quote Originally Posted by Paul_Hossler View Post
    Might be better to post this in the Outlook forum or ask one of the mods to move it for you
    But I need the macro in Excel, not Outlook

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Sorry - misunderstood. Should have read it more carefully
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    To make a start:

    Sub M_snb()
        sn = ActiveSheet.Columns(1).CurrentRegion
    
        With CreateObject("Outlook.Application")
            For j = 2 To UBound(sn)
                c00 = "[Start] ='" & Format(sn(j, 3), "ddddd h:mm") & "' And [Subject]='" & sn(j, 1) & "'"
                If .GetNamespace("MAPI").GetDefaultFolder(9).Items.Find(c00).Start = "" Then
            
                With .createitem(1)
                     .Subject = sn(j, 1)
                    .Location = sn(j, 2)
                    .Start = sn(j, 3)
                    .Duration = sn(j, 4)
                    .BusyStatus = 2
                    If sn(j, 5) <> "" Then .BusyStatus = sn(j, 5)
                    .ReminderSet = False
                    If sn(j, 6) > 0 Then
                        .ReminderSet = True
                        .ReminderMinutesBeforeStart = sn(j, 6)
                    End If
                    .Body = sn(j, 7)
                    .Save
                End With
                End If
            Next
        End With
    End Sub
    You can find more over here: http://www.snb-vba.eu/VBA_Outlook_external_en.html#L6

  6. #6
    When I use the code above, I get an error:

    Run-time error '91':
    Object variable or With block variable not set

    When I click Debug, the line highlighted is
    If .GetNamespace("MAPI").GetDefaultFolder(9).Items.Find(c00).Start = "" Then

    I have the Microsoft Outlook 15.0 Object Library enabled.

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Makes sense:
    Sub M_snb() 
        on error resume next
        sn = ActiveSheet.Columns(1).CurrentRegion 
         
        With CreateObject("Outlook.Application") 
            For j = 2 To UBound(sn) 
                c00 = "[Start] ='" & Format(sn(j, 3), "ddddd h:mm") & "' And [Subject]='" & sn(j, 1) & "'" 
                c01=.GetNamespace("MAPI").GetDefaultFolder(9).Items.Find(c00).Start
                if err.number<>0 then 
                    With .createitem(1) 
                        .Subject = sn(j, 1) 
                        .Location = sn(j, 2) 
                        .Start = sn(j, 3) 
                        .Duration = sn(j, 4) 
                        .BusyStatus = 2 
                        If sn(j, 5) <> "" Then .BusyStatus = sn(j, 5) 
                        .ReminderSet = False 
                        If sn(j, 6) > 0 Then 
                            .ReminderSet = True 
                            .ReminderMinutesBeforeStart = sn(j, 6) 
                        End If 
                        .Body = sn(j, 7) 
                        .Save 
                    End With 
                    err.clear
                End If 
            Next 
        End With 
    End Sub

  8. #8
    That's excellent, thank you

Posting Permissions

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