[Lazarus] RE : Intergrating with office suites

Ludo Brands ludo.brands at free.fr
Sat Jan 7 17:58:22 CET 2012


 

-----Message d'origine-----
De : Chris Kelling [mailto:kellingc at cox.net] 
Envoyé : samedi 7 janvier 2012 16:05
À : Lazarus mailing list
Objet : [Lazarus] Intergrating with office suites



I’m trying to integrate a couple of functions that integrate with MS  Office
(and/or Open Office).  One is using an existing Word template for generating
a completed form, and the other is trying to use Outlook to email reports.
Does anyone have a resource that I can use to figure out how to do this?

 

-Chris 

 

The easiest is to use COM with late binding. Sounds difficult but it isn't.
Example to send an email with attachment:

 

uses ComObj;

...

procedure TForm1.Button1Click(Sender: TObject);
const olMailItem=0;

var Mail,Message:variant;

 

begin
  // attach to running outlook
  Mail:=GetActiveOleObject('Outlook.Application');
  Message:=Mail.CreateItem(olMailItem);
  Message.Recipients.Add('first at recipient');
  Message.Recipients.Add('second at recipient');
  Message.Subject:= 'This is the subject';
  Message.Body:= 'Hi from lazarus';
  Message.CC:='cc at recipient';
  Message.Attachments.Add('C:\lazarus\readme.txt') ;
  Message.Send();
end;


The example assumes outlook is already running. You can also start up
Outlook by doing:

Mail:=CreateOleObject('Outlook.Application');

 
To know what you can do with this com object, google for
'Outlook.Application' and you'll find hundreds of examples, mainly using VB
or VBA. The methods and properties are however the same. Or use the
reference manual at
http://msdn.microsoft.com/en-us/library/aa210897%28v=office.11%29.aspx. 
 
Similar, to "automate" word do a
GetActiveOleObject('Word.Application');
 
Automating openoffice is possible but you'll find far less help for this. 
 
An alternative is to use early binding. In latest fpc trunk, you'll find a
typelib import utility (importtl) that creates the bindings for com
typelibs. The advantage is that early binding is much faster, most errors
are found at compile time and all constants (olMailItem in example) and
record types are imported from the typelib.  
In any case, use a recent compiler (2.6.0) to get this working (both late
and early binding). Older compilers had some problems with COM objects.
 
Ludo
 




 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20120107/9f4824d7/attachment-0003.html>


More information about the Lazarus mailing list