Dear developers,
I need to write 300 manuals in Word for a product, the supplier delivers them in powerpoint, so I thought lets automate this!
I have written procedure that looks very logic and does work, only not as expected.
The Images and text are collected from Powerpoint and inserted in a wordt document, the only thing is that all images are at the beginning of the document and all text is at the bottom of the document.
I rather have it in order the way I collected it from the slides in powerpoint.
For example:
- Slide 1 holds 2 images and 3 text rows
- Slide 2 holds 1 image and 2 text rows
The output should be:
- Slide1.Image 1
- Slide1.Image 2
- Slide1.Text 1
- Slide1.Text 2
- Slide1.Text 3
- Slide2.Image 1
- Slide2.Text 1
- Slide2.Text 2
Now the layout is:
- Slide1.Image 1
- Slide1.Image 2
- Slide2.Image 1
- Slide1.Text 1
- Slide1.Text 2
- Slide1.Text 3
- Slide2.Text 1
- Slide2.Text 2
So I am doing something wrong, and I cannot find how to fix this.
Is there someone who would like to help me out here?
I am using a windows system with lazarus and office365 at a local install.
Here is the code:
procedure TForm1.AddAllContentFromPowerPointToWord(const PowerPointPath, WordPath: string);
var
PowerPointApp, Presentation, Slide, Shape, WordApp, WordDoc, WordRange: Variant;
i, j: Integer;
TempImagePath: string;
begin
try
// Start Microsoft PowerPoint
PowerPointApp := CreateOleObject('PowerPoint.Application');
PowerPointApp.Visible := True;
// Open PowerPoint-presentation
Presentation := PowerPointApp.Presentations.Open(PowerPointPath, False, False, True);
// Start Microsoft Word
WordApp := CreateOleObject('Word.Application');
WordApp.Visible := True;
// Open the wordt document
WordDoc := WordApp.Documents.Open(WordPath);
// Loop to all slides
for i := 1 to Presentation.Slides.Count do
begin
Slide := Presentation.Slides.Item(i);
// Loop all shapes on slide
for j := 1 to Slide.Shapes.Count do
begin
Shape := Slide.Shapes.Item(j);
WordRange := WordDoc.Content;
WordRange.Collapse(0);
WordRange.InsertAfter(sLineBreak);
if Shape.HasTextFrame and (Shape.TextFrame.HasText = True) then
begin
//it is text
WordRange.InsertAfter(Shape.TextFrame.TextRange.Text + sLineBreak);
end
else
begin
//it is a picture
TempImagePath := IncludeTrailingPathDelimiter(GetTempDir) + 'Image' + IntToStr(i) + '_' + IntToStr(j) + '.jpg';
Shape.Copy;
WordApp.Selection.Paste;
end;
end;
WordRange := WordDoc.Content;
WordRange.Collapse(0);
WordRange.InsertAfter(sLineBreak);
WordApp.InsertAfter(sLineBreak);
end;
// Opslaan en sluiten van het Word-document
WordDoc.Save;
WordDoc.Close(False);
WordApp.Quit;
// Sluit de PowerPoint-presentatie en PowerPoint
Presentation.Close;
PowerPointApp.Quit;
except
on E: Exception do
Writeln('Er is een fout opgetreden: ', E.Message);
end;
end;
I thank everyone for reading!
Hopefully someone can see what I do wrong.
Best regards, Mischa