0

I am creating an appointment in Outlook through java code. here I can set new values to the fields in appointment. The code for it is

OleAutomation appointment = invoke(outlook, "CreateItem", 1).getAutomation();
appointment.setProperty(property(appointment, "Subject"), new Variant("Test"));

this code will set the subject field with the value "Test".

here i am using generic OLE mechanism "Variant" for passing data of different types via a common interface Now I want to know how to set a date for the appointment. Please help me..

Thanks in advance :)

Anu
  • 1,884
  • 14
  • 30
  • This may help you: http://stackoverflow.com/questions/2599102/oledate-java-implementation. Dates are stored as floats indicating the number of days since 30 december 1899 at midgnight. – Alex Mar 16 '12 at 08:16
  • how to get the OLE automation date in java? – Anu Mar 16 '12 at 08:45

1 Answers1

0

As stated in my comment, the date in OLE are stored as the number of days since 1899-12-30. You can calculate that number easily. When you have it, pass-it to to the right OLE property using standard variant I think.

Unfortunately I don't have SWT installed here and can't test the code, but it should look something like that

public class Test {
    static Calendar OLE_BASE_DATE = Calendar.getInstance();
    static {
        OLE_BASE_DATE.set(1899, 11, 30); // 1899-12-30
    }

    static double oleDateFormat(Calendar cal) {
        long diff = cal.getTimeInMillis() - OLE_BASE_DATE.getTimeInMillis();
        return diff / 86400000L;
    }

    public static void main(String[] args) {
        // get outlook instance etc...
        OleAutomation appointment = invoke(outlook, "CreateItem", 1).getAutomation();
        appointment.setProperty(property(appointment, "Subject"), new Variant("Test"));
        // compute the appointment start & stop
        double todayAtNoon = oleDateFormat(Calendar.getInstance()) + 0.5;
        double todayAt13_12 = oleDateFormat(Calendar.getInstance()) + 0.55;
        // set the vars
        appointment.setProperty(property(appointment, "Start"), new Variant(String.valueOf(todayAtNoon)));
        appointment.setProperty(property(appointment, "End"), new Variant(String.valueOf(todayAt13_12)));
        appointment.setProperty(property(appointment, "Location"), new Variant("At foo's"));
        // do more stuff
    }
}

More info on the automation of Outlook can be found here for a complete example in VB

Alex
  • 25,147
  • 6
  • 59
  • 55
  • Hi Alex, Thank You for your code to get oleDateFormat. There is a small correction in the prgm, that we should use the date as float as below appointment.setProperty(property(appointment, "Start"), new Variant((float)todayAtNoon)); appointment.setProperty(property(appointment, "End"), new Variant((float)todayAt13_12)); – Anu Mar 16 '12 at 10:07
  • Thnx for sharing @Alex , I've been searching for how to invoke a meeting appointment there must be a header for that, have you ever tried that? – E_X Dec 24 '14 at 08:12
  • @E_X I think you need to replace `CreateItem` with `AppointmentItem`. You can take a look here: http://msdn.microsoft.com/en-us/library/ms268893.aspx and here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.appointmentitem.aspx – Alex Dec 24 '14 at 15:48
  • @Alex I will check it and let you know :) – E_X Dec 25 '14 at 23:17
  • @Alex I tried the replacements as you suggested which results a NullPointerException, tried invoking it with other numbers (0, 1, 2 ..etc) still the same Exception, I am inspecting the links you sent me though, it seems one should use CreateItem and another property after it. – E_X Dec 28 '14 at 07:04
  • @Alex Any advise is much appreciated. – E_X Feb 05 '15 at 13:56