分享

Random thoughts on coding & technology: OLE Automation with Delphi part I

 quasiceo 2013-12-01

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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:

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多