2010. november 24., szerda

Create a message in MS Outlook using OLE


Problem/Question/Abstract:

How can I create a new message in MS Outlook using OLE?

Answer:

const
  olMailItem = 0;
var
  Outlook: OLEVariant;
  MailItem: Variant;
begin
  try
    Outlook := GetActiveOleObject('Outlook.Application');
  except
    Outlook := CreateOleObject('Outlook.Application');
  end;

  MailItem := Outlook.CreateItem(olMailItem);
  MailItem.Recipients.Add('mshkolnik@scalabium.com');
  MailItem.Subject := 'your subject';
  MailItem.Body := 'Welcome to my homepage: http://www.scalabium.com';
  MailItem.Attachments.Add('C:\Windows\Win.ini');
  MailItem.Send;

  Oulook := Unassigned;
end;

I can save tasks and contacts to Outlook using OLE, but I need to be able to synchronize existing contacts. Do you have any ideas?

I think that you have a two methods with solutions

1.

var
  app, NameSpace, Contact: OLEVariant;
begin
  app := CreateOleObject(`Outlook.Application`);
  NameSpace := app.GetNameSpace(`MAPI`);
  Contact := NameSpace.GetItemFromID(EntryIDItem, EntryIDStore)
    {...}
end;

2. also you can navigate by contract items and compare the IDs. I understood that it`s not a good solution but without errors:)

app := CreateOleObject(`Outlook.Application`);
Contacts := app.GetDefaultFolder(10); {olFolderContacts}
for i := 0 to Items.Count - 1 do
begin
  Contact := Items[i];
  {...}
end;

How can I format the content of the body text to be HTML?

If you want to have html formatted message, use HTMLBody property instead Body. But not that this property is available starting from Outlook 98 only.

I need to parse through a certain folder's messages and put some data into a database based off what is in the  messages.  Suggestions?

Check my articles about MS Outlook programming:

http://www.scalabium.com/faq/dct0120.htm
http://www.scalabium.com/faq/dct0121.htm
http://www.scalabium.com/faq/dct0123.htm
and download a Delphi sample for these articles:
http://www.scalabium.com/faq/delphioutlook.zip

I try to execute this procedure in my program, i get the following error: project project1 raised exception class EOleSysError with message 'CoInitialize not called'.

You must call the OLEInitialize(nil) procedure from ComCtrls.pas unit. Some third-party suites unload this library from memory. Also additioanlly you must call it from every your thread if you'll use an OLE from this thread.

Nincsenek megjegyzések:

Megjegyzés küldése