How to pass appointments from a Shared Calendar To personal Calendar in a Outlook Macro?

Posted on 2007-08-06
Last Modified: 2013-11-05
We want to be able to automatize the process of copy all the appointments in a shared Calendar to the personal calendar, since the only way to do this is selecting the appointments on the shared calendar and copy them to the personal calendar. we tried to use macros to do that but the macro recorder is not avaliable in outlook so we need to use a vbscript for the macro...
Question by:perifel
    LVL 76

    Expert Comment

    by:David Lee
    Hi, perifel.

    That's doable, but it does present a problem.  Copying all the appontments from the shared to the personal calendar the first time is simple.  After that things become more difficult.  Unless the personal calendar is always going to be just an exact copy of the shared calendar, then you're faced with synchronizing the two calendars or with devising a way to erase the items from the shared calendar before the next run.  Synchronizing isn't a simple process.  It's possible, but it's complicated.  If you don't want to sync, then finding a way to delete the old appointments from the shared calendar is a must.  Othewise, you end up with duplicate appointments.  Deleting the appointments from the shared calendar is a lot easier than syncing, but it'll still require some planning.  If you want to go this route, non-syncing, then I can provide the code.  If you want to sync, then I'll be glad to offer suggestions, but that's too involved for me to produce the code.

    Author Comment

    Thanks for your coments!

    It would be great if you can give us, both, the code for the non-sync  way, and the suggestions to the sync way, so we can work it out, and find the solution that fits the best for us.

    Thanks in advance.
    LVL 76

    Accepted Solution

    Sorry to be slow getting back to you.  The code for the non-sync solution is below.  I haven't tested it, but it should work.  It works by deleting all of the shared calendar appointments from the private calendar and then copy the shared appointments to the private calendar.  The delete process depends on the categories field of each shared calendar entry containing the word "SharedCalendar".  If you don't want to enter that word as you create each entry, then we could modify the code to insert it for you as it copies each item.

    Sub CopyAppointmentsFromFolder()
        Dim olkSource As Outlook.MAPIFolder, _
            olkDest As Outlook.MAPIFolder, _
            olkAppt As Outlook.AppointmentItem, _
            olkCopy As Outlook.AppointmentItem, _
            intIndex As Integer
        'Change the folder path on the following line to that of the shared calendar
        Set olkSource = OpenOutlookFolder("Path")
        'Change the folder path on the following line to that of the private calendar
        Set olkDest = OpenOutlookFolder("Path")
        'Delete the shared calendar appointments from the private calendar
        For intIndex = olkDest.Items.Count To 1 Step -1
            Set olkAppt = olkDest.Items.Item(intIndex)
            If InStr(1, olkAppt.Categories, "SharedCalendar") Then
            End If
        'Copy the shared calendar items to the private calendar
        For Each olkAppt In olkSource.Items
            Set olkCopy = olkAppt.Copy
            olkCopy.Move olkDest
        Set olkCopy = Nothing
        Set olkAppt = Nothing
        Set olkDest = Nothing
        Set olkSource = Nothing
    End Sub
    LVL 76

    Assisted Solution

    by:David Lee
    The syncing solution depends on a key field you can use to match items.  The problem is that Outlook has no such field.  Yes, each item in Outlook does have a unique identifier called EntryID.  The problem is that the value of EntryID is based on the container the item is in.  Thus, item X will have an EntryID of Y in one container while an exact copy of X will have an EntryID of Z in a different container.  That makes EntryID unusable for matching items in a sync routine.  The Subject field won't work since it's possible to have any number of items with the same Subject.  Date/time fields won't work, since item X and a copy of Item X will always have different dates/times since one has to have been written after the other.  So, we have to create a field to match and sync items and figure a way to assign unique values to that field from either container.  Creating the field is no problem.  Filling it in is the problem.  The best solution would be to use a web service so you get the IDs from a single source.  That may not be possible though if you have notebooks that might be used while disconnected from the network and therefore unable to access the web service.  Another possibility is to assign a computer specific prefix to the ID.  All records from computer XYZ would have a unique ID begining with XYZ.

    The second issue is how to handle deletions.  Adds and updates are no problem since in both cases the item is there.  For an Add there'll be a record in one container and not the other.  We simply need to copy the record to the other container.  For an Update the same record will exist in both containers, but will be newer in one than the other.  We overwrite the older record with the newer one.  Deletes pose a problem since they look like an Add, a record exists in one container but not the other.  But, we don't want to copy the record to the container it's missing from, we want to delete it.  So, how can we tell when there's been a delete?  One way is to mark records for deletion rather than actually deleting them immediately.  Outlook doesn't support that natively though.  Another way would be to move records we want to delete to a separate folder and have the sync routine process records in it as a deletion.  However, that requires teaching everyone to delete properly.  Better to have a means that doesn't require special actions by the user.  The only way I can think of to solve this is to write a journal of actions.  We'd need to write code that traps delete events and writes a transaction journal.  The sync routine then reads the journal as part of its process.

    A third issue is how to handle changes to the same record in both containers.  User A changes the phone number of record X in one container while user B changes the address of record X in the other container.  When the sync runs oen fo those changes will be lost because we only know that the record changed, not what was changed in it.  The last change made will overwrite the earlier change.  If the address change was entered last, then the phone number change will get lost altogether.  The journaling idea will solve this too.  When a record is changed the journaling action will record what was changed in the record.  That way both the address and phone number changes will be preserved.  Journaling isn't built into Outlook, so this will require writing your own from scratch.  It'll require trapping add, change, and delete events, recording what was changed on a per field basis, and writing that information to a file that the sync routine can use later.  It's doable, but it'll require an investment in time and effort to get it working.  

    Author Comment


    We´ve already made a couple of scripts that solve th problem in a non sync way. the main diference with the code you gave us, is that we "peek" into the shared calendar and only copy the appointments that are necesary, also we use some restrictions in the script so it have a better performance, (In calendar with many appointments the response time was too long)

    Thanks for your help an if someone wants the code we made, contact us ;).
    LVL 76

    Expert Comment

    by:David Lee
    You're welcome.  Glad I was able to help out.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Learn more about the importance of email disclaimers with our top 10 email disclaimer DOs and DON’Ts.
    Email signatures have numerous marketing benefits. Here are 8 top reasons to turn your email signature into a marketing channel.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now