Wednesday, 24 February 2010

OLE Automation with Delphi part I

Today I'm going to publish about a little application I've been developing recently for one of my clients. This little application pretends to use the Microsoft word OLE Automation to build a document with incrustation of images in it. But Why have I used OLE automation instead of working with the TLB functionality?. That's simple, because working with the OLE can help me with any of the Microsoft documents, and I recommend to work with the TLB when you are sure that you'll be working with a concrete package of Microsoft Office. For example, with the Office 2007, you can use the version 12.0 of the Object Library for your own purpose (but having in mind that you have to follow the license rules) .

The major problem working with OLE is that's difficult to know how exactly do the things that you want. In my case I wanted to insert some stretched pictures inside a word document in a correct position. After seeking out all the information, I've managed to build this little application that will help you to encrust images inside the document.

If we load all the images and try to generate the document, the selected images will be encrusted like the next image:

Notice that It works with word 2003 or 2007 (but I've found some problems with the 2007). Anyway, the little code for encrust an image to a word document would be like this:

procedure EncrustImage();
var
   WordApplication, WordDocument, CurrentPic: Variant;
   cnt, rgn: variant;
begin
   try
       WordApplication := CreateOleObject('Word.Application');
       WordDocument := WordApplication.Documents.Open(Edit1.Text);

       //get to the last position of the caret
       cnt := WordDocument.Characters.Count;
       cnt := cnt - 1;
       rgn := WordDocument.Range(Start := cnt, end := cnt);

       if path1.text <> '' then
       begin
           rgn.InlineShapes.AddPicture(path1.text);
           CurrentPic := WordApplication.ActiveDocument.InlineShapes.Item(1);
           Currentpic.Height := 170.0; // points
           Currentpic.Width := 225.0;
       end;

       cnt := WordDocument.Characters.Count;
       cnt := cnt - 1;
       rgn := WordDocument.Range(Start := cnt, end := cnt);

       if path2.text <> '' then
       begin
           rgn.InlineShapes.AddPicture(path2.text);
           CurrentPic := WordApplication.ActiveDocument.InlineShapes.Item(2);
           Currentpic.Height := 170.0; // points
           Currentpic.Width := 225.0;
       end;

       cnt := WordDocument.Characters.Count;
       cnt := cnt - 1;
       rgn := WordDocument.Range(Start := cnt, end := cnt);
       rgn.Text := chr(13) + chr(10);

       WordDocument.SaveAs(FileName := Edit2.text + '\out' + ExtractFileName(Edit1.text), AddToRecentFiles := False);
       WordApplication.Quit(False);
   except
       WordApplication.Quit(False);
   end;
end;


You can find this little application here:
Related Articles:

3 comments:

  1. Ohh Nice one. really i found some of very important tips. thanks for sharingDelphi

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi Millan,
    I'm glad you liked it, and thanks for comment.

    ReplyDelete