如何添加图片到PowerPoint演示文稿图片PlaceHolder?

我在Excel VBA中创build了一些代码,为Excel的每一行创build一个PowerPoint演示文稿1幻灯片,并在PowerPoint中的特定文本框中填充。
我现在要添加所有匹配描述的图像。 这些都是Jpegs而不是图表等
我怎样才能做到这一点,在Excel中做这个更好,还是做Powerpoint VBA本身更好?
无论如何,任何人都可以帮我一些关于如何做到这一点的代码吗?
PowerPoint中已经存在图像帧。 每张幻灯片有2张图片(没有转换或任何东西)。
谢谢!

PS我在Windows 7上使用PowerPoint和Excel 2010。


有没有办法从Excel做到这一点? 我的代码的其余部分是在Excel中,作为macros的一部分,这将是很好的。
基本上我有一个文件的位置,我想要使用例如C:\ insertfoldername \ imagename.jpeg出现在电子表格(大约400行)的H列。
我正在使用的Powepoint模板有一个图像框架(Powerpoint中的那个,你把鼠标hover在它上面。“从文件插入图片”。
这些已经大小,并在正确的位置。
我想要做的是,在Excel中,将图像从Excel中的文件path粘贴到特定的图像框架中。
这是可能的吗?

基本上这样做的东西:
PPT.ActivePresentation.Slides(2).Shapes(3)LoadImage(spath)

以下是我正在使用的代码。
****表示文件path。 jpg文件被设置为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 

这是如何使用Excel在当前打开的PPT Picture PlaceHolders添加图片。
我们使用Early Binding添加Microsoft PowerPoint 14.0 Object Library参考。

编辑1:添加DoEvents和一些解释

 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 

总结一下:
1.我们掌握了PPT的应用
我们抓住幻灯片和幻灯片中的形状
3.现在我们select只有ppPlaceholderPicturetypes的形状
4.我们使用Shape Object's (ppPlaceholderPicturetypes) .Top, .Left, .Width and .Height属性作为Shapes Collection的 .AddPicture方法的参数。

在那里,你已经在你的PPT图片占位符中添加了一张图片。
希望这是你所需要的。

虽然这看起来像是在将图像添加到具有空白图片或内容占位符的幻灯片中时起作用,但它将始终进入该占位符并resize以适应。

你只需要像这样添加它:

 osld.Shapes.AddPicture "Path", msoFalse, msoTrue, -1, -1