Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

loading a bunch of JPG images in MsWord with delphi

Posted on 2008-10-15
10
Medium Priority
?
1,467 Views
Last Modified: 2013-11-10
Hi experts,

I have around 200 jpg images (each one about 200x200 pixels) and I want to insert all of them in a MsWord document. I am sure this can be done programatically in Delphi using OLE automation with Word. In fact, I use this OLE technique to spell check a word document with some peculiar technique.

I don't know the code to ask word thru delphi how to read a jpg file, insert it in the document, then next the read one until the jpg files are exhausted. My jpg files are conveniently numbered in an attempt to do more eawily this kind of program.

Any ideas?

best regards
Lopem (manuel lopez)
0
Comment
Question by:lopem
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 10

Expert Comment

by:atul_parmar
ID: 22728429
I guess you can enumerate your jpg files.

var
  ci: TCreateInfo;
begin
  ci.CreateType := ctFromFile;
  ci.ShowAsIcon := False;
  ci.FileName := 'document1.doc'; // the word document where you want to insert pictures
  ci.ClassID := ProgIdToClassId('Word.Document.8');
  OleContainer1.CreateObjectFromInfo(ci);
  OleContainer1.DoVerb(0);
  // enumerate your jpgs and excute the following line for each jpg
  OleContainer1.OleObject.Shapes.AddPicture('c:\tpt.jpg');
end;
0
 
LVL 3

Author Comment

by:lopem
ID: 22737758
hi atul parmar,

I tried your code, adding the necessary units, but I am getting an EOleSysErr: cannot find %1...

I´m using delphi 7, word 2003 and win xp. What I am doing wrong? this is the whole code...

Thanks in advance.
Manuel Lopez, (lopem)

unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, OleCtnrs, comobj, Word2000, OleServer;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    OleContainer1: TOleContainer;
    WordApplication1: TWordApplication;
    WordDocument1: TWordDocument;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
var
  ci: TCreateInfo;
begin
  ci.CreateType := ctFromFile;
  ci.ShowAsIcon := False;
  ci.FileName := 'document1.doc'; // the word document where you want to insert pictures
  ci.ClassID := ProgIdToClassId('Word.Document.8');
  OleContainer1.CreateObjectFromInfo(ci);
  OleContainer1.DoVerb(0);
  // enumerate your jpgs and excute the following line for each jpg
  OleContainer1.OleObject.Shapes.AddPicture('sol001.jpg');
end;
 
end.

Open in new window

0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 22738046
>>  ci.FileName := 'document1.doc';
Provide a valid file name with full path. e.g.  'c:\document1.doc' which MUST exist.
>> OleContainer1.OleObject.Shapes.AddPicture('sol001.jpg');
Same applies to above line also.

Atul
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:birbilis
ID: 22738931
Use Word's Macro Recording feature and start recording, then execute the action you want (insert image) once, then stop recording. Then show the VBA toolbar (right click on empty space at toolbars and select that toolbar) and open the VBA code editor (not the VSA one that also exists at newer Office) then see the VBA code the macro recorder generated. Convert that code to Delphi (easy to do). If it uses any constants "wdXX" then press F2 at VBA to see the Object Explorer of VBA and search for that constant to see its value and define it as a constant in your Delphi program too to be able to use it in your code. Here's some VBScript code you can easily convert to Object Pascal / Delphi:
 
c o n s t   w d G o T o P a g e  =  1
c o n s t   w d G o T o N e x t  =  2
w o r d . S e l e c t i o n . G o T o   w d G o T o P a g e ,   w d G o T o N e x t ,   1 
w o r d . S e l e c t i o n . I n l i n e S h a p e s . A d d P i c t u r e ( f i l e n a m e ,   F a l s e ,   T r u e ) 

Open in new window

0
 
LVL 3

Author Comment

by:lopem
ID: 22746442
Hi atul parmar,

I did what you said and the code almost works!  but I found two problems:

i. For some reason only one Image is loaded to the olecontainer1
ii. I can't do anything with that, I mean, the doc1.doc exists but it seems it never holds the picture loaded. In other words, there is no way to save even the jpg file loaded to the olecontainer1.

In fact, I feel a little bit embarrased with you. For me what you're doing is something really new and I want to understand what it is going on.

Regards
Manuel Lopez (lopem)
0
 
LVL 3

Author Comment

by:lopem
ID: 22746444
Oops, forgot to include the code...
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,  OleCtnrs, comobj, Word2000, OleServer, StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    OleContainer1: TOleContainer;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
var
  ci: TCreateInfo;
begin
  ci.CreateType := ctFromFile;
  ci.ShowAsIcon := False;
  ci.FileName := 'c:\users\morsa\software_morsa\imgs2msword\doc1.doc'; // the word document where you want to insert pictures
  ci.ClassID := ProgIdToClassId('Word.Document.8');
  OleContainer1.CreateObjectFromInfo(ci);
  OleContainer1.DoVerb(0);
  // enumerate your jpgs and excute the following line for each jpg
  OleContainer1.OleObject.Shapes.AddPicture('c:\users\morsa\software_morsa\imgs2msword\zagreb01.jpg');
  OleContainer1.OleObject.Shapes.AddPicture('c:\users\morsa\software_morsa\imgs2msword\zagreb02.jpg');
end;
 
end.

Open in new window

0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 22755382
>>i. For some reason only one Image is loaded to the olecontainer1

For me all images are loaded (I tried with 2); one thing I observed is the second one overlaps the first so it seems that only one image is loaded. It would require to position it as per your needs. Read http://support.microsoft.com/kb/198508 on how you can load and position.

>>ii. I can't do anything with that, I mean, the doc1.doc exists but it seems it never holds the picture loaded. In other words, there is no way to save even the jpg file loaded to the olecontainer1.

You can save the document using OleContainer1.DoVerb(1). Or use the attached snippet wherein I bind the oleobject to word interface.

http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_20854224.html

procedure TForm1.Button1Click(Sender: TObject);
var
  ci: TCreateInfo;
  doc : _Document;
begin
  ci.CreateType := ctFromFile;
  ci.ShowAsIcon := False;
  ci.FileName := 'c:\Project.doc'; // the word document where you want to insert pictures
  ci.ClassID := ProgIdToClassId('Word.Document.8');
  OleContainer1.CreateObjectFromInfo(ci);
  OleContainer1.DoVerb(0);
  // enumerate your jpgs and excute the following line for each jpg
  OleContainer1.OleObject.Shapes.AddPicture('c:\tpt.jpg');
  OleContainer1.OleObject.Shapes.AddPicture('c:\annualday_GIIS_pune.jpg');
  //
  OleContainer1.OleObjectInterface.QueryInterface(_Document, doc);
  if doc <> nil then
  begin
    doc.Save;
  end;
end;

Open in new window

0
 
LVL 10

Accepted Solution

by:
atul_parmar earned 800 total points
ID: 22755390
Add Word97 or Word2000 in your uses list for above snippet to work.
0
 
LVL 3

Author Closing Comment

by:lopem
ID: 31506596
Atul,
This thing using an OLE container was really a revelaqtion. Where I can find more about this subject?

In any case, now I know why you have the GURU rank!

regards
Manuel López (lopem)
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 22764804
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question