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

标签 vba excel powerpoint

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

P.S 我在 Windows 7 上使用 PowerPoint 和 Excel 2010。


有没有办法从 Excel 中执行此操作?我的其余代码位于 Excel 中,如果能将其作为宏的一部分就太好了。
基本上我有一个我想使用的文件位置,例如C:\insertfoldername\imagename.jpeg 出现在电子表格的 H 列中(大约 400 行)。
我正在使用的 Powerpoint 模板具有图像框架(Powerpoint 中的图像框架,当您将鼠标悬停在它上面时会显示......“从文件中插入图片”。
它们的尺寸已经确定并且位于正确的位置。
我想要做的是,在 Excel 中,从 Excel 中的文件路径粘贴图像并将其粘贴到特定的图像框架中。
这可能吗?

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

下面是我正在使用的代码。
**** 表示文件路径。 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

最佳答案

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

你只需要像这样添加它:

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

关于vba - 如何将图片添加到Powerpoint演示文稿图片占位符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23049917/

相关文章:

vba - 在 VBA 中返回,不符合我的预期

excel - VBA:具有多个操作的单行 if 语句

vba - 将项目添加到 VBA/VB6 集合时出错

excel - 带有符号 Excel 的设计下拉菜单

vba - 在 powerpoint 中编辑嵌入对象

python-pptx:有什么办法使文本垂直?

excel - 获取表格列 Excel VBA 的范围(或值)

javascript - 如何在浏览器中显示excel图表?

excel - 由于数字格式为文本,将 Excel 文件读取到 Python 失败

vba - 在 VBA 中使用 powerpoint 的撤消命令