Question:
How can I close the CD Tray in code?
Answer:
Make a call to the MMSystem function mciSendCommand 
sending the MCI_SET_DOOR_CLOSED command.
Example:
uses MMSystem;
procedure CloseCD(Drive : char);
var
 mp : TMediaPlayer;
begin
 result := false;
 Application.ProcessMessages;
 mp := TMediaPlayer.Create(nil);
 mp.Visible := false;
 mp.Parent := Application.MainForm;
 mp.Shareable := true;
 mp.DeviceType := dtCDAudio;
 mp.FileName := Drive + ':';
 mp.Open;
 Application.ProcessMessages;
 mciSendCommand(mp.DeviceID, 
 MCI_SET, MCI_SET_DOOR_CLOSED, 0);
 Application.ProcessMessages;
 mp.Close;
 Application.ProcessMessages;
 mp.free;
 result := true;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
 CloseCD('D');
end;