Manuel Lopez-Michelone
asked on
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)
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)
ASKER
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)
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.
>> ci.FileName := 'document1.doc';
Provide a valid file name with full path. e.g. 'c:\document1.doc' which MUST exist.
>> OleContainer1.OleObject.Sh apes.AddPi cture('sol 001.jpg');
Same applies to above line also.
Atul
Provide a valid file name with full path. e.g. 'c:\document1.doc' which MUST exist.
>> OleContainer1.OleObject.Sh
Same applies to above line also.
Atul
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 )
ASKER
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)
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)
ASKER
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.
>>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.
https://www.experts-exchange.com/questions/20854224/How-do-I-Import-a-Bitmap-into-a-Word-Doc-programatically.html
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.
https://www.experts-exchange.com/questions/20854224/How-do-I-Import-a-Bitmap-into-a-Word-Doc-programatically.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;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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)
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)
For OLE I suggest http://www.blong.com/Articles/Automation%20In%20Delphi/Automation.htm
and for Word (document) reference you can use MSDN.
http://msdn.microsoft.com/de-de/library/microsoft.office.interop.word._document(VS.80).aspx
Atul
and for Word (document) reference you can use MSDN.
http://msdn.microsoft.com/de-de/library/microsoft.office.interop.word._document(VS.80).aspx
Atul
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.Docu
OleContainer1.CreateObject
OleContainer1.DoVerb(0);
// enumerate your jpgs and excute the following line for each jpg
OleContainer1.OleObject.Sh
end;