本文介绍了如何添加图片到Powerpoint演示文稿PictureHolder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Excel VBA中创建了一些代码来为Excel的每一行创建一个PowerPoint演示文稿1个幻灯片,并填入PowerPoint中的一个特定文本框。

我现在要添加所有的图像符合说明。这些都是Jpegs而不是图表等。

我如何做到这一点,最好在excel中这样做,还是更好的做这个Powerpoint VBA本身?

无论如何,任何人都可以帮助我解决一些关于如何做的代码?

图像帧已经存在于PowerPoint中。每张幻灯片有两个图像(没有过渡或任何东西)。

谢谢!



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






有没有办法通过Excel?我的代码的其余部分是在Excel中,作为宏的一部分,这是很好的。

基本上我有一个文件位置,我想使用例如C:\insertfoldername\imagename.jpeg出现在电子表格中的H列中(约400行)。

我使用的Powepoint模板具有图像框架(Powerpoint中的一个,您将鼠标悬停在在这里说..插入图片从文件。

这些已经大小,并在正确的位置。

我想做的是,在Excel中,粘贴图像从excel中的文件路径,并将其过滤到特定的Image框架。

这是否可以完成?



基本上这样做:

PPT.ActivePresentation.Slides(2).Shapes(3)LoadImage(spath)



以下是我正在使用的代码。

****表示文件路径,jpg文件设置为excel电子表格中的第3列。 >

  Sub CreateSlides()
'Dim the Excel objects
Dim objWorkbook As New Excel.Workbook
Dim objWorksheet作为Excel.Worksheet

'缩小文件路径字符串
D im 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

设置PPT = GetObject(,PowerPoint.Application)

PPT .Visible = True

'获取第一张幻灯片的布局并设置一个CustomLayout对象
设置pptLayout = PPT.ActivePresentation.Slides(1).CustomLayout

'运行OpenFile函数以获取打开文件对话框。它返回一个包含文件和路径的String。
strFilePath = OpenFile()

'打开Excel文件
设置objWorkbook = Excel.Application.Workbooks.Open(strFilePath)

'抓住工作簿中的第一个工作表
设置objWorksheet = objWorkbook.Worksheets(1)

'循环遍历列A中的每个使用的行
对于i = 2 To objWorksheet.Range(A65536 ).End(xlUp).Row

设置PPT = GetObject(,PowerPoint.Application)

设置pptNewSlide = PPT.ActivePresentation.Slides.AddSlide(PPT。 ActivePresentation.Slides.Count + 1,pptLayout)

'获取当前行中使用的列数
Dim LastCol As Long
Dim boldWords As String

boldWords =Line1:,line2:,Line3:,Line4:
LastCol = objWorksheet.Rows(i).End(xlToRight).Column
如果LastCol = 16384 Then LastCol = 1 '因为某些原因,如果只有列1有数据,它返回16384,所以正确它

'构建行
上的所有列的字符串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****这是jpg名称

设置PPT = GetObject(PowerPoint.Application)

spath =C:\test\\ \\

'将字符串写入幻灯片
pptNewSlide.Shapes(2).TextFrame.TextRange.Text = objWorksheet.Cells(i,3).Value'这将进入电影标题
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

'这是我想在图像中加载的地方。
'PPT.ActivePresentation.Slides(PPT.ActivePresentation.Slides.Count).Shapes(3).Picture = LoadPicture(spath)'& ($)
$ b'PPT.ActivePresentation.Slides(2)。 c>






 函数OpenFile()
'Dim文件对话框对象和字符串
Dim objFileDialog As FileDialog
Dim strFile As String

'将objFileDialog设置为FileDialog对象的一个​​实例
设置objFileDialog =应用程序。 FileDialog(msoFileDialogFilePicker)

'设置objFileDialog对象的属性
objFileDialog.AllowMultiSelect = False
objFileDialog.ButtonName =选择
objFileDialog.InitialView = msoFileDialogViewDetails
objFileDialog.Title =选择Excel文件
objFileDialog.InitialFileName =C:\
objFileDialog.Filters.Clear
objFileDialog.Filters.AddExcel,* .xls; * .xlsx,1
objFileDialog.FilterIndex = 1

'显示FileDialog框
objFileDialog.Show

'将strFile设置为SelectedItems的第一个记录我们的FileDialog的操作
strFile = objFileDialog.SelectedItems(1)

'返回文件路径字符串
OpenFile = strFile
结束函数


解决方案

这是在当前打开的PPT中添加图片的方式 Picture PlaceHolders 使用Excel。

我们使用 Early Binding 添加 Microsoft PowerPoint 14.0对象库 reference。



Edit1:添加DoEvents和一些说明

  Sub ImportPictureInPlaceHolderFromExcel()

Dim oPPt As PowerPoint.Application
Dim oPPtSlide As PowerPoint.Slide
Dim oPPtShp As PowerPoint.Shape

'~~>获取PPt实例,表示您当前打开的PPT演示文稿
设置oPPt = GetObject(,Powerpoint.Application)
'~~>参考应包含图片占位符的第一张幻灯片
设置oPPtSlide = oPPt.ActivePresentation.Slides(1)

'~~>现在检查幻灯片中的每个形状
对于每个oPPtShp在oPPtSlide.Shapes
'~~>你只需要在图片占位符上工作
如果oPPtShp.PlaceholderFormat.Type = ppPlaceholderPicture然后
与oPPtShp
'~~>现在添加图片
'~~>对于此示例,图像路径在单元格A1
oPPtSlide.Shapes.AddPicture范围(A1)。值,msoFalse,msoTrue,_
.Left,.Top,.Width,.Height
'~~>在这里插入DoEvents专为大文件或网络文件
'~~> DoEvents暂时停止宏直到
'~~>系统完成正在加载的图片文件
DoEvents
结束
结束如果
下一个

设置oPPtSlide = Nothing
设置oPPt = Nothing

End Sub

总结:

1.我们掌握PPT应用程序

2.我们抓住幻灯片和幻灯片中的形状

3.现在我们选择 ppPlaceholderPicture 仅形式的形状

4.我们使用 Shape对象的(ppPlaceholderPicture类型) .Top,.Left,.Width和.Height 属性作为形状集合 .AddPicture 方法。



你去,你已经在PPT图片占位符中添加了图片。

希望这是你需要。


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!

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


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?

Basically something that will do this:
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.

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
解决方案

This is how you add pictures in currently open PPT Picture PlaceHolders using Excel.
We used Early Binding adding the Microsoft PowerPoint 14.0 Object Library reference.

Edit1: Adding DoEvents and some explanation

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 ppPlaceholderPicture type only
4. We use the Shape Object's(ppPlaceholderPicture type) .Top, .Left, .Width and .Height property as argument for Shapes Collection's .AddPicture method.

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

这篇关于如何添加图片到Powerpoint演示文稿PictureHolder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:17