vba 如何将图片添加到PowerPoint演示文稿图片占位符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23049917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:45:57  来源:igfitidea点击:

How to add pictures to Powerpoint Presentation Picture PlaceHolder?

excelvbaexcel-vbapowerpointpowerpoint-vba

提问by pixie

I have created some code in Excel VBA to create a PowerPoint presentation 1 slide for each row of Excel, and populate in a specific text box in PowerPoint.
I now want to add in all the images that match the description. These are all Jpegs and not charts etc.
How can I do this, and is it better to do this in excel, or is it better to do this Powerpoint VBA itself?
Eitherway, would anyone be able to help me out with some code on how to do this please?
The image frames already exist in PowerPoint. There are 2 images per slide (no transitions or anything).
Thank you!

我在 Excel VBA 中创建了一些代码来为 Excel 的每一行创建一个 PowerPoint 演示文稿 1 张幻灯片,并填充在 PowerPoint 中的特定文本框中。
我现在想添加与描述匹配的所有图像。这些都是 Jpegs 而不是图表等。
我该怎么做,在 excel 中这样做更好,还是自己做这个 Powerpoint VBA 更好?
不管怎样,有人能帮我写一些关于如何做到这一点的代码吗?
图像框架已存在于 PowerPoint 中。每张幻灯片有 2 张图像(没有过渡或任何东西)。
谢谢!

P.S I am using PowerPoint and Excel 2010 on Windows 7.

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



Is there a way to do this from Excel? The rest of my code is in Excel and It would be great to do that as part of the Macro.
Basically I have a file location that I want to use e.g. C:\insertfoldername\imagename.jpeg appears in column H in the spreadsheet (about 400 rows).
The Powepoint template I am using has the image frame (The one in Powerpoint, that wehn you hover over it says.."Insert Picture from file".
These are already sized and are in the right location.
What I want to do is, in Excel, Paste the image from the file path that is in excel and past it into that specific Image frame.
Is that going to be possible at all?

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

Basically something that will do this:
PPT.ActivePresentation.Slides(2).Shapes(3)LoadImage(spath)

基本上可以做到这一点:
PPT.ActivePresentation.Slides(2).Shapes(3)LoadImage(spath)

Below is the code I am using.
**** Indicates the File path. the jpg file is set as the 3rd column in the excel spreadsheet.

下面是我正在使用的代码。
**** 表示文件路径。jpg 文件设置为 Excel 电子表格中的第 3 列。

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

采纳答案by L42

This is how you add pictures in currently open PPT Picture PlaceHoldersusing Excel.
We used Early Bindingadding the Microsoft PowerPoint 14.0 Object Libraryreference.

这是您Picture PlaceHolders使用 Excel在当前打开的 PPT 中添加图片的方式。
我们使用Early Binding添加Microsoft PowerPoint 14.0 Object Library引用。

Edit1:Adding DoEvents and some explanation

Edit1:添加 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

To sum-up:
1. We get hold of PPT application
2. We get hold of the slide and the shapes within the slide
3. Now we choose shapes which are ppPlaceholderPicturetype only
4. We use the Shape Object's(ppPlaceholderPicture type) .Top, .Left, .Width and .Heightproperty as argument for Shapes Collection's.AddPicturemethod.

总结:
1. 我们掌握了 PPT 应用程序
2. 我们掌握了幻灯片和幻灯片中的形状
3. 现在我们选择ppPlaceholderPicture只有类型的形状
4. 我们使用Shape Object's(ppPlaceholderPicture type).Top, .Left, .Width and .Height属性作为参数形状集合的.AddPicture方法。

And there you go, you've added a picture in your PPT Picture Placeholder.
Hope this is what you need.

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

回答by John Wilson

While that looks likeit works when you add an image to a slide with an empty picture or content placeholder it will always go into that placeholder and resize to fit.

虽然当您将图像添加到带有空图片或内容占位符的幻灯片时,这看起来很有效,但它始终会进入该占位符并调整大小以适合。

You just need to add it like this:

你只需要像这样添加它:

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