Recent

Author Topic: Converting Powerpoint to word  (Read 329 times)

tcm2803

  • Newbie
  • Posts: 2
Converting Powerpoint to word
« on: July 29, 2024, 11:02:31 am »
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:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.AddAllContentFromPowerPointToWord(const PowerPointPath, WordPath: string);
  2. var
  3.   PowerPointApp, Presentation, Slide, Shape, WordApp, WordDoc, WordRange: Variant;
  4.   i, j: Integer;
  5.   TempImagePath: string;
  6. begin
  7.   try
  8.     // Start Microsoft PowerPoint
  9.     PowerPointApp := CreateOleObject('PowerPoint.Application');
  10.     PowerPointApp.Visible := True;
  11.  
  12.     // Open PowerPoint-presentation
  13.     Presentation := PowerPointApp.Presentations.Open(PowerPointPath, False, False, True);
  14.  
  15.     // Start Microsoft Word
  16.     WordApp := CreateOleObject('Word.Application');
  17.     WordApp.Visible := True;
  18.  
  19.     // Open the wordt document
  20.     WordDoc := WordApp.Documents.Open(WordPath);
  21.  
  22.     // Loop to all slides
  23.     for i := 1 to Presentation.Slides.Count do
  24.     begin
  25.       Slide := Presentation.Slides.Item(i);
  26.       // Loop all shapes on slide
  27.       for j := 1 to Slide.Shapes.Count do
  28.       begin
  29.         Shape := Slide.Shapes.Item(j);
  30.         WordRange := WordDoc.Content;
  31.         WordRange.Collapse(0);
  32.         WordRange.InsertAfter(sLineBreak);
  33.         if Shape.HasTextFrame and (Shape.TextFrame.HasText = True) then
  34.         begin
  35.           //it is text
  36.           WordRange.InsertAfter(Shape.TextFrame.TextRange.Text + sLineBreak);
  37.         end
  38.         else
  39.         begin
  40.           //it is a picture
  41.           TempImagePath := IncludeTrailingPathDelimiter(GetTempDir) + 'Image' + IntToStr(i) + '_' + IntToStr(j) + '.jpg';
  42.           Shape.Copy;
  43.           WordApp.Selection.Paste;
  44.         end;
  45.       end;
  46.       WordRange := WordDoc.Content;
  47.       WordRange.Collapse(0);
  48.       WordRange.InsertAfter(sLineBreak);
  49.       WordApp.InsertAfter(sLineBreak);
  50.     end;
  51.  
  52.     // Opslaan en sluiten van het Word-document
  53.     WordDoc.Save;
  54.     WordDoc.Close(False);
  55.     WordApp.Quit;
  56.  
  57.     // Sluit de PowerPoint-presentatie en PowerPoint
  58.     Presentation.Close;
  59.     PowerPointApp.Quit;
  60.   except
  61.     on E: Exception do
  62.       Writeln('Er is een fout opgetreden: ', E.Message);
  63.   end;
  64. end;
  65.  

I thank everyone for reading!
Hopefully someone can see what I do wrong.

Best regards, Mischa
« Last Edit: July 29, 2024, 11:07:36 am by tcm2803 »

Jorg3000

  • Jr. Member
  • **
  • Posts: 69
Re: Converting Powerpoint to word
« Reply #1 on: July 29, 2024, 11:45:51 am »
Hi!
I think WordRange.Collapse(0) is always set to the beginning of the document, which leads to a wrong order of elements.

Try the following code instead your main loop. Unfortunately, I can't test it myself. Perhaps it is an approach.

Code: Pascal  [Select][+][-]
  1. // Loop to all slides
  2.     for i := 1 to Presentation.Slides.Count do
  3.     begin
  4.       Slide := Presentation.Slides.Item(i);
  5.       // Loop all shapes on slide
  6.       for j := 1 to Slide.Shapes.Count do
  7.       begin
  8.         Shape := Slide.Shapes.Item(j);
  9.         if Shape.HasTextFrame and (Shape.TextFrame.HasText = True) then
  10.         begin
  11.           //it is text
  12.           WordApp.Selection.TypeText(Shape.TextFrame.TextRange.Text + sLineBreak);
  13.         end
  14.         else
  15.         begin
  16.           //it is a picture
  17.           Shape.Copy;
  18.           WordApp.Selection.Paste;
  19.           WordApp.Selection.TypeParagraph;
  20.         end;
  21.       end;
  22.       WordApp.Selection.TypeParagraph;
  23.     end;
  24.  

tcm2803

  • Newbie
  • Posts: 2
Re: Converting Powerpoint to word
« Reply #2 on: July 29, 2024, 12:07:36 pm »
Hello Jorg3000,

That has fixed the problem! Thank you very much!

A new question popped up, how can I find what type a Shape has?
The Arrow Shape from Powerpoint is now read as a Image, can I split those?

Best regards, Mischa

 

TinyPortal © 2005-2018