Jump to content

add a picture to a powerpoint slide


Recommended Posts

  • Replies 41
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

For me this script works as expected.
Store the following script in the same folder where my PowerPoint UDF resides and you will see what I mean:

#include <PowerPoint.au3>

Global Const $msoBringForward = 2 ; Bring shape forward.
Global Const $msoBringInFrontOfText = 4 ; Bring shape in front of text.
Global Const $msoBringToFront = 0 ; Bring shape to the front.
Global Const $msoSendBackward = 3 ; Send shape backward.
Global Const $msoSendBehindText = 5 ; Send shape behind text.
Global Const $msoSendToBack = 1 ; Send shape to the back.

$oPPT = _PPT_Open()
$oPres = _PPT_PresentationOpen($oPPT, @ScriptDir & "\Presentation1.pptx")
$oSlide = $oPres.Slides(1)
$oPicture = _PPT_SlideAddPicture($oSlide, @ScriptDir & "\Microsoft_PowerPoint_2013_logo.png", -1, -1, 721, 542)
$oPicture.ZOrder($msoSendToBack)
MsgBox(0, "Result", "@error = " & @error & ", @extended = " & @extended)

Func _PPT_SlideAddPicture(ByRef $obj, $filepath, $left = 0, $top = 0, $width = 100, $height = 100)
    If IsObj($obj) <> 1 Then
        SetError(1)
        Return 0
    ElseIf FileExists($filepath) <> 1 Then
        SetError(2)
        Return 2 ;file does not exist
    ElseIf $left = "" Or $top = "" Or $width = "" Or $height = "" Then
        SetError(3)
        Return 3
    ElseIf IsInt($left+$top+$width+$height) <> 1 Then
        SetError(4)
        Return 4 ;All parameters have to be integer
    Else
        Return $obj.Shapes.AddPicture($filepath, 0, 1,150, 150, 500, 350)
    Endif
EndFunc

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

trying the save as function. i remove the format default. 

regarding the filepath, the below code created a folder 111 and then save the file as Slide1.jpg

_PPT_PresentationSaveAs($oPres, "H:\Auto\111", 17)

the saveas method seems to be below 

SaveAs( Filename, FileFormat, EmbedFonts )

Filename - Specifies the name to save the file under. If you don't include a full path, PowerPoint saves the file in the current folder.

if i try the below, then nothing happens

_PPT_PresentationSaveAs($oPres, "123.jpg"", 17)

Func _PPT_PresentationSaveAs($oPresentation, $sFilePath, $iFormat, $bOverWrite = Default)
    If Not IsObj($oPresentation) Or ObjName($oPresentation, 1) <> "_Presentation" Then Return SetError(1, 0, 0)
    If $iFormat = Default Then
        $iFormat = $ppSaveAsDefault
    Else
        If Not IsNumber($iFormat) Then Return SetError(2, 0, 0)
    EndIf
    If $bOverWrite = Default Then $bOverWrite = False
    If FileExists($sFilePath) Then
        If Not $bOverWrite Then Return SetError(3, 0, 0)
        Local $iResult = FileDelete($sFilePath)
        If $iResult = 0 Then Return SetError(4, 0, 0)
    EndIf
    $oPresentation.SaveAs($sFilePath, $iFormat)
    If @error Then Return SetError(5, @error, 0)
    Return 1
EndFunc   ;==>_PPT_PresentationSaveAs

Link to comment
Share on other sites

If something goes wrong with a _PPT* function then @error and @extended get set. What are the values after calling _PPT_presentationSaveAs?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Please give this a try:

_PPT_PresentationSaveAs($oPres, "123", 17)

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Seems PPT needs the full path:
 

_PPT_PresentationSaveAs($oPres, @ScriptDir & "\123", 17)

creates a subdirectory (named "123") in the current script directory and saves each slide to a new JPG.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

i was trying the above code before but it will save each slider to a new jpg. since we did not indicate the filename each time, how does it know ?

i see that the saveas function overwrite is false so it should not overwrite. i was expecting it to save the second time as Slide2.jpg

Link to comment
Share on other sites

i was trying to loop this but when the loop was running the second time, it just crashed with below. couldn't see any errors

"H:\Auto\Test.au3" (17) : ==> The requested action with this object has failed.:
$oPicture.ZOrder(1)
$oPicture^ ERROR

 

$Photo = 1

while $Photo < 6
$oPPT = _PPT_Open()
$oPres = _PPT_PresentationOpen($oPPT, @ScriptDir & "\Test.ppt")
MouseClick($MOUSE_CLICK_LEFT,1000,600,1)
Send("{DEL 1}")
$oSlide = $oPres.Slides(1)
$oPicture = _PPT_SlideAddPicture($oSlide, @ScriptDir & "\" & $Photo & ".jpg", -1, -1, 721, 542)
MsgBox(0,"111",$Photo)
$oPicture.ZOrder(1)

_PPT_PresentationSaveAs($oPres, @ScriptDir&"\New", 17)

;_PPT_PresentationSaveAs($oPres, @ScriptDir & "\New" & "\" & $Photo & ".jpg", 17)
_PPT_PresentationClose($oPres)
_PPT_Close($oPPT)
$Photo = $Photo + 1
MsgBox(0,"111",$Photo)
WEnd

Link to comment
Share on other sites

  1. You do not see any errors because you do not ask for the error information. Check @error and @extended after _PPT_SlideAddPicture.
  2. Do not combine automating PowerPoint by UDF and by sending MouseClicks etc. to the GUI. This rarely works.
    What do you try to do with Send and MouseClick?
     

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

the mouseclick is to select the picture in the ppt and then delete it away before i add the new picture. quick and dirty.

i removed the sending mouse and keystroke but it still crashed the second time in the loop. it crashed after $oPicture.Zorder(1). before that, error and extended are both 0.

$Photo = 1
while $Photo < 6
$oPPT = _PPT_Open()
$oPres = _PPT_PresentationOpen($oPPT, @ScriptDir & "\Test.ppt")
$oSlide = $oPres.Slides(1)
$oSlide.Shapes(1).Delete

$oPicture = _PPT_SlideAddPicture($oSlide, @ScriptDir & "\" & $Photo & ".jpg", -1, -1, 721, 542)
;MsgBox(0,"111",$Photo)

MsgBox(0, "Result", "@error = " & @error & ", @extended = " & @extended)

$oPicture.ZOrder(1)
MsgBox(0, "Result", "@error = " & @error & ", @extended = " & @extended)

_PPT_PresentationSaveAs($oPres, @ScriptDir&"\New", 17)

;_PPT_PresentationSaveAs($oPres, @ScriptDir & "\New" & "\" & $Photo & ".jpg", 17)
_PPT_PresentationClose($oPres)
_PPT_Close($oPPT)
$Photo = $Photo + 1
;MsgBox(0,"222",$Photo)
WEnd

 

Link to comment
Share on other sites

@error = 3 means that the output file already exists. Set parameter $bOverWrite  to True.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

that error is gone now but somehow the script still crashed

@@ Trace(32) :    ;MsgBox(0,"111",$Photo)
>Error code: 0

@@ Trace(35) :    $oPicture.ZOrder(1)
"H:\Auto\Test.au3" (35) : ==> The requested action with this object has failed.:
$oPicture.ZOrder(1)
$oPicture^ ERROR
->20:53:04 AutoIt3.exe ended.rc:1
+>20:53:04 AutoIt3Wrapper Finished.
>Exit code: 1    Time: 6.042

Link to comment
Share on other sites

Do you check @error and @extended after calling _PPT_SlideAddPicture?
It seem that $oPicture is not an object.

Check this by

$oPicture = _PPT_SlideAddPicture($oSlide, @ScriptDir & "\" & $Photo & ".jpg", -1, -1, 721, 542)
MsgBox(0, "_PPT_SlideAddPicture", "@error = " & @error & ", @extended = " & @extended & ", IsObj = " & IsObj($oPicture))

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

i think i found the problem. need to use a new picture object everytime but since it is an object variable. how do i make use of a new variable everytime the loop runs ?

using an if else doesn't seems right

if $Photo = 2 Then
$oPicture = _PPT_SlideAddPicture($oSlide, @ScriptDir & "\" & $Photo & ".jpg", -1, -1, 721, 542)
$oPicture.ZOrder(1)
Else
$oPicture1 = _PPT_SlideAddPicture($oSlide, @ScriptDir & "\" & $Photo & ".jpg", -1, -1, 721, 542)
$oPicture1.ZOrder(1)
EndIf

Link to comment
Share on other sites

looks okay now. i use array for the picture object. but one flaw is the saveas can't seems to work without creating a new folder everytime even putting in a full path

$Photo = 1
Local $oPicture[5]
while $Photo < 6
$oPPT = _PPT_Open()
$oPres = _PPT_PresentationOpen($oPPT, @ScriptDir & "\Test.ppt")
$oSlide = $oPres.Slides(1)
$oSlide.Shapes(1).Delete
$oPicture[$Photo] = _PPT_SlideAddPicture($oSlide, @ScriptDir & "\" & $Photo & ".jpg", -1, -1, 721, 542)
$oPicture[$Photo].ZOrder(1)

_PPT_PresentationSaveAs($oPres, @ScriptDir&"\New" & $Photo, 17)
_PPT_PresentationClose($oPres)
_PPT_Close($oPPT)
$Photo = $Photo + 1
WEnd

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...