excel - 如何将excel中的图片复制到PPT的形状?
问题描述
我试试这段代码,从excel复制到ppt:
Dim presentation As Object
Set ppt = CreateObject("PowerPoint.Application")
Set presentation = ppt.Presentations.Open2007("D:\temp.pptx", MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue)
Dim oSlide As Object
Set oSlide = presentation.Slides(7)
Dim oSheet As Worksheet
Set oSheet = ThisWorkbook.Sheets(2)
Dim oImageOb As Object
Set oImageOb = oSheet.Shapes(1)
oImageOb.Copy
oSlide.Shapes.PasteSpecial DataType:=2
但 PPT 执行后退出PasteSpecial
。
如何将excel中的图片复制到PPT的形状?
解决方案
为了在 PowerPoint 中将图像粘贴到指定的形状中,有一些注意事项:
- Shape 必须是允许图像的类型,例如某些内容占位符。您不能将图像插入文本框、图表占位符等。
- 必须
Select
编辑形状。虽然我们习惯于告诉人们避免在 Excel VBA中使用Select
orActivate
,但是在 PowerPoint 和 Word 中,某些操作只能在对象处于视图中和/或选中时执行。为了Select
形状,我们需要Select
幻灯片。
我已经通过将变量声明移到顶部来清理您的过程,并修改了路径/幻灯片索引等。我创建了一个新变量pptShape
,我们将使用它来处理幻灯片上的特定形状实例。
请注意,我已经更改了路径和幻灯片/形状索引。
Option Explicit
Sub foo()
Dim ppt As Object 'PowerPoint.Application
Dim oSlide As Object 'PowerPoint.Slide
Dim pptShape As Object 'PowerPoint.Shape
Dim oImageOb As Object
Dim oSheet As Worksheet
Dim pres As Object 'PowerPoint.Presentation
Set ppt = CreateObject("PowerPoint.Application")
Set pres = ppt.Presentations.Open2007("c:\debug\empty ppt.pptx", MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue)
Set oSlide = pres.Slides(3)
Set oSheet = ThisWorkbook.Sheets(1) ' ## MODIFY AS NEEDED
Set oImageOb = oSheet.Shapes(1) ' ## MODIFY AS NEEDED
oImageOb.Copy
Set pptShape = oSlide.Shapes(1) ' ## MODIFY AS NEEDED
'## to preserve aspect ratio and prevent stretching/skewing image:
pptShape.Width = oImageOb.Width
pptShape.Height = oImageOb.Height
' ## Select the slide
oSlide.Select
' ## Selct the shape
' ## NOTE: This shape MUST be of a type that contains a picture frame, otherwise
' an error will occur
pptShape.Select
' ## All of the following methods work for me:
'ppt.CommandBars.ExecuteMso "PasteJpeg"
'ppt.CommandBars.ExecuteMso "PasteBitmap"
'ppt.CommandBars.ExecuteMso "PasteAsPicture"
ppt.CommandBars.ExecuteMso "Paste"
End Sub
这是我的带有图像的 Excel 表:
输出,将图像粘贴到适当的图像占位符中:
推荐阅读
- java - Cucumber+DSL+Http+Spring - 是否可以在 Scenario 与 citrus 框架之间共享相同的 TestRunner?
- python - Pandas 从 dataframe_b 的所有列中减去 dataframe_a 中的每一列,并将结果写入第三个数据帧
- python - 获取某人的头像并将其用于我的个人资料 discord.py
- python - Scrapy 不会消耗所有 start_urls
- javascript - Javascript find the closest number in an array of objects and retrieve the object's key value
- xml - 将变量中的值连接到 xml 文件中
- flutter - 通过 Google 登录获取生日和性别在颤动中失败
- java - 尝试运行我的 Spring Boot 应用程序 JAR 时无法确定合适的驱动程序类
- python - 为什么我在 CodeChef 中收到运行时 NZEC 错误?
- python - 如何用python制作一个列表循环