Solved

loading a bunch of JPG images in MsWord with delphi

Posted on 2008-10-15
10
1,381 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 200 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now