HOWTO: Create Task and Appointment Items with Outlook Object Model and Visual J++
ID: Q216726
 
  | 
The information in this article applies to:
- 
Microsoft Visual J++, version  6.0
 
- 
Microsoft Outlook  98
 
SUMMARY
This article contains sample code demonstrating how to use the Outlook Object Model (OOM) with Visual J++ to create a Task item and an Appointment item.
The article also shows how to use Java's Time class to set up some "time-related" properties of the Task and Appointment items.
MORE INFORMATION
Please use the following steps to build and run the sample code:
- Start Visual J++ 6.0 and create a new Console Application project.
 
- In the Project Explorer window, open your Project tree and double-click on the Class1.java file created for you.
 
- From the Project menu, select the Add COM Wrapper, click Outlook 98 Type Library, and click OK.
 
- Ignore the created code and replace it with the following code. Make sure you update the code whenever you see the TO DO comment.
 
-  Compile and run the code. 
 
Sample Code
import com.ms.com.*;	
import com.ms.wfc.ui.*;   //in order to use MessageBox
import com.ms.wfc.app.*;  //in order to use Time object
import msoutl85.*;        //in order to use OOM
public class Class1
{
  public static void main(String[] args)
  {
    _Application olApp = (_Application) new msoutl85.Application();
    //app get confused with app of com.ms.wfc.app.*, so we add msoutl85
    //otherwise we can use the next line instead!
    //_Application ol = (_Application) new Application(); 	
    NameSpace mapiNS = olApp.GetNamespace("MAPI");
	
    //Create a null parameter for later use
    Variant nullParam = new Variant();
    nullParam.noParam();
	
    //Create a profile
    Variant profile = new Variant();
    profile.putString("<ProfileName>"); //TO DO: change to your profile
    mapiNS.Logon(profile, nullParam, nullParam, nullParam);
		
    //Create a parameter for Display use later
    Variant model = new Variant();
    model.putBoolean(true);
    //Get the Tasks folder
    MAPIFolder oTaskFolder = mapiNS.GetDefaultFolder(OlDefaultFolders.olFolderTasks);
    System.out.println("Task Folder Name : " + oTaskFolder.getName());	
    //oTaskFolder.Display();  
	
    //Create a new task in the folder oFolder
    try
    {	
	//Create a task
	_TaskItem newTask = null;
	newTask = (_TaskItem)oTaskFolder.getItems().Add(nullParam);
	
	//set up some properties for the new task
	newTask.setSubject("This is a test");
	newTask.setBody("How are you doing today?");
	
	Time tm = new Time(1999, 1, 30); //year, month, day
	newTask.setStartDate(tm.toDouble());
	newTask.setDueDate(tm.toDouble());
		
	//Save
	newTask.Save();
	//newTask.Display(model);
         newTask = null;
    }
    catch(java.lang.Exception e) 
    { 
	System.out.println( "Logon failed!" ); 
	e.printStackTrace(); 
    } 
 		
    //get the Calendar folder
    MAPIFolder oCalFolder = mapiNS.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
    System.out.println("Folder Name : " + oCalFolder.getName());
    //MessageBox.show(oCalFolder.getName());
    //oCalFolder.Display();
    //create an appointment in the Calendar folder
    try
    {
	//create an appointment
	_AppointmentItem oNewAppt = null;
	oNewAppt = (_AppointmentItem)oCalFolder.getItems().Add(nullParam);
		
	//sep up some properties
	oNewAppt.setSubject("Another Test");
	oNewAppt.setBody("Hello! Again!");
		
	Time tmStart = new Time("02/1/1999 08:00 am");
	Time tmEnd = new Time("02/1/1999 10:00 am");
	oNewAppt.setStart(tmStart.toDouble());
	oNewAppt.setEnd(tmEnd.toDouble());
	
	//oNewAppt.setAllDayEvent(true); 
	oNewAppt.setAllDayEvent(false);
	oNewAppt.setLocation("LPB");
		
	//save
	oNewAppt.Save();
	//oNewAppt.Display(model);
         oNewAppt = null;		
    }
    catch(java.lang.Exception e) 
    { 
      System.out.println( "Logon failed!" ); 
      e.printStackTrace(); 
    } 		
    System.out.println("Program terminated");
	
    //clean up
    oTaskFolder = null;
    oCalFolder =  null;
    olApp = null;
    mapiNS = null;
  }
}
 
REFERENCES
For more information about using the Outlook Object Model with Visual J++, please see the following article in the Microsoft Knowledge Base:
Q216728 How to Send Message using Outlook Object Model with Visual J++
Additional query words: 
kbMsg kbOutlook kbOutlook850 kbOutlook98 kbOutlookObj kbJava 
Keywords          : kbJava kbMsg kbOutlook kbOutlook98 kbOutlookObj kbOutlook850 
Version           : WINDOWS:6.0,98
Platform          : WINDOWS 
Issue type        : kbhowto