Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1516
  • Last Modified:

loading a bunch of JPG images in MsWord with delphi

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
lopem
Asked:
lopem
  • 5
  • 4
1 Solution
 
atul_parmarCommented:
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
 
lopemAuthor Commented:
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
 
atul_parmarCommented:
>>  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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
birbilisCommented:
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
 
lopemAuthor Commented:
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
 
lopemAuthor Commented:
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
 
atul_parmarCommented:
>>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
 
atul_parmarCommented:
Add Word97 or Word2000 in your uses list for above snippet to work.
0
 
lopemAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now