Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

loading a bunch of JPG images in MsWord with delphi

Posted on 2008-10-15
10
Medium Priority
?
1,481 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Progress

886 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