Comment ajouter des images au Picture PlaceHolder de présentation Powerpoint ?
-
21-12-2019 - |
Question
J'ai créé du code dans Excel VBA pour créer une présentation PowerPoint 1 diapositive pour chaque ligne d'Excel et remplir une zone de texte spécifique dans PowerPoint.
Je souhaite maintenant ajouter toutes les images qui correspondent à la description.Ce sont tous des fichiers Jpeg et non des graphiques, etc.
Comment puis-je faire cela, et est-il préférable de le faire dans Excel, ou est-il préférable de faire ce Powerpoint VBA lui-même ?
Quoi qu'il en soit, quelqu'un pourrait-il m'aider avec un code sur la façon de procéder, s'il vous plaît ?
Les cadres d'images existent déjà dans PowerPoint.Il y a 2 images par diapositive (pas de transitions ou quoi que ce soit).
Merci!
P.S. J'utilise PowerPoint et Excel 2010 sous Windows 7.
Existe-t-il un moyen de faire cela depuis Excel ?Le reste de mon code est dans Excel et ce serait formidable de le faire dans le cadre de la macro.
Fondamentalement, j'ai un emplacement de fichier que je souhaite utiliser, par exemple.C:\insertfoldername\imagename.jpeg apparaît dans la colonne H de la feuille de calcul (environ 400 lignes).
Le modèle Powepoint que j'utilise a le cadre d'image (celui de Powerpoint, que lorsque vous survolez, dit .. "Insérer une image à partir d'un fichier".
Ceux-ci sont déjà dimensionnés et se trouvent au bon endroit.
Ce que je veux faire, c'est, dans Excel, coller l'image à partir du chemin du fichier qui se trouve dans Excel et la coller dans ce cadre d'image spécifique.
Est-ce que cela sera possible ?
En gros, quelque chose qui fera ceci :
PPT.ActivePresentation.Slides(2).Shapes(3)LoadImage(spath)
Ci-dessous le code que j'utilise.
**** Indique le chemin du fichier.le fichier jpg est défini comme 3ème colonne dans la feuille de calcul Excel.
Sub CreateSlides()
'Dim the Excel objects
Dim objWorkbook As New Excel.Workbook
Dim objWorksheet As Excel.Worksheet
'Dim the File Path String
Dim strFilePath As String
'Dim the PowerPoint objects
Dim PPT As Object
Dim pptSlide As PowerPoint.Slide
Dim pptLayout As PowerPoint.CustomLayout
Dim pptNewSlide As PowerPoint.Slide
Dim str As String
Dim Title As String
Set PPT = GetObject(, "PowerPoint.Application")
PPT.Visible = True
'Get the layout of the first slide and set a CustomLayout object
Set pptLayout = PPT.ActivePresentation.Slides(1).CustomLayout
'Run the OpenFile function to get an Open File dialog box. It returns a String containing the file and path.
strFilePath = OpenFile()
'Open the Excel file
Set objWorkbook = Excel.Application.Workbooks.Open(strFilePath)
'Grab the first Worksheet in the Workbook
Set objWorksheet = objWorkbook.Worksheets(1)
'Loop through each used row in Column A
For i = 2 To objWorksheet.Range("A65536").End(xlUp).Row
Set PPT = GetObject(, "PowerPoint.Application")
Set pptNewSlide = PPT.ActivePresentation.Slides.AddSlide(PPT.ActivePresentation.Slides.Count + 1, pptLayout)
'Get the number of columns in use on the current row
Dim LastCol As Long
Dim boldWords As String
boldWords = "Line1: ,line2: ,Line3: ,Line4: "
LastCol = objWorksheet.Rows(i).End(xlToRight).Column
If LastCol = 16384 Then LastCol = 1 'For some reason if only column 1 has data it returns 16384, so correct it
'Build a string of all the columns on the row
str = ""
str = "Line1: " & str & objWorksheet.Cells(i, 1).Value & Chr(13) & _
"Line2: " & objWorksheet.Cells(i, 2).Value & Chr(13) & _
"Line3: " & objWorksheet.Cells(i, 10).Value & Chr(13) & _
"Line4: " & objWorksheet.Cells(i, 7).Value & Chr(13) & Chr(13) & _
objWorksheet.Cells(i, 14).Value
sfile = Cells(i, 3) & ".jpg" **** This is the jpg name
Set PPT = GetObject(, "PowerPoint.Application")
spath = "C:\test\"
'Write the string to the slide
pptNewSlide.Shapes(2).TextFrame.TextRange.Text = objWorksheet.Cells(i, 3).Value 'This enters the film Title
PPT.ActivePresentation.Slides(PPT.ActivePresentation.Slides.Count).Shapes(1).TextFrame.TextRange.Text = str
BoldSomeWords PPT.ActivePresentation.Slides(PPT.ActivePresentation.Slides.Count).Shapes(1), str, boldWords
'This is where I want to load in the Image.
'PPT.ActivePresentation.Slides(PPT.ActivePresentation.Slides.Count).Shapes(3).Picture = LoadPicture(spath) ' & sfile)
'PPT.ActivePresentation.Slides(2).Shapes(3)LoadImage((spath))
Next
End Sub
Function OpenFile()
'Dim the File Dialog object and string
Dim objFileDialog As FileDialog
Dim strFile As String
'Set the objFileDialog to an instance of the FileDialog object
Set objFileDialog = Application.FileDialog(msoFileDialogFilePicker)
'Set the Properties of the objFileDialog object
objFileDialog.AllowMultiSelect = False
objFileDialog.ButtonName = "Select"
objFileDialog.InitialView = msoFileDialogViewDetails
objFileDialog.Title = "Select Excel File"
objFileDialog.InitialFileName = "C:\"
objFileDialog.Filters.Clear
objFileDialog.Filters.Add "Excel", "*.xls; *.xlsx", 1
objFileDialog.FilterIndex = 1
'Show the FileDialog box
objFileDialog.Show
'Set strFile to the first record of the SelectedItems property of our FileDialog
strFile = objFileDialog.SelectedItems(1)
'Return the File Path string
OpenFile = strFile
End Function
La solution
Voici comment ajouter des images dans un PPT actuellement ouvert Picture PlaceHolders
en utilisant Excel.
Nous avons utilisé Early Binding
en ajoutant le Microsoft PowerPoint 14.0 Object Library
référence.
Edit1 : Ajout de DoEvents et quelques explications
Sub ImportPictureInPlaceHolderFromExcel()
Dim oPPt As PowerPoint.Application
Dim oPPtSlide As PowerPoint.Slide
Dim oPPtShp As PowerPoint.Shape
'~~> Get hold of PPt instance meaning your currently open PPT presentation
Set oPPt = GetObject(, "Powerpoint.Application")
'~~> Reference the first slide which should contain picture placeholders
Set oPPtSlide = oPPt.ActivePresentation.Slides(1)
'~~> Now check each shape in slide
For Each oPPtShp In oPPtSlide.Shapes
'~~> You only need to work on Picture place holders
If oPPtShp.PlaceholderFormat.Type = ppPlaceholderPicture Then
With oPPtShp
'~~> Now add the Picture
'~~> For this example, picture path is in Cell A1
oPPtSlide.Shapes.AddPicture Range("A1").Value, msoFalse, msoTrue, _
.Left, .Top, .Width, .Height
'~~> Insert DoEvents here specially for big files, or network files
'~~> DoEvents halts macro momentarily until the
'~~> system finishes what it's doing which is loading the picture file
DoEvents
End With
End If
Next
Set oPPtSlide = Nothing
Set oPPt = Nothing
End Sub
Pour résumer:
1.Nous récupérons l'application PPT
2.Nous récupérons la diapositive et les formes à l'intérieur de la diapositive
3.Maintenant, nous choisissons des formes qui sont ppPlaceholderPicture
tapez seulement
4.Nous utilisons le Shape Object's
(type ppPlaceholderPicture) .Top, .Left, .Width and .Height
propriété comme argument pour Collection de formes .AddPicture
méthode.
Et voilà, vous avez ajouté une image dans votre espace réservé pour l'image PPT.
J'espère que c'est ce dont vous avez besoin.
Autres conseils
Alors que ressemble à cela fonctionne lorsque vous ajoutez une image à une diapositive avec une image vide ou un espace réservé au contenu, elle ira toujours dans cet espace réservé et sera redimensionnée pour s'adapter.
Il vous suffit de l'ajouter comme ceci :
osld.Shapes.AddPicture "Path", msoFalse, msoTrue, -1, -1