简体   繁体   English

插入图像图像占位符excel vba powerpoint

[英]insert image image place holder excel vba powerpoint

I am programming an excel VBA macro, which generates a powerpoint presentation from other presentations. 我正在编程一个Excel VBA宏,该宏从其他演示文稿生成一个PowerPoint演示文稿。 Finally a content-slide is generated. 最后,生成内容幻灯片。 Depending on the topics the script should copy the related pictures of this topic to image place holders. 根据主题,脚本应将该主题的相关图片复制到图像占位符。 At the moment the content slide is populated with the correct text boxes (topic names), but inserting the images does not work. 目前,内容幻灯片中填充了正确的文本框(主题名称),但无法插入图像。

Intentend pseudo-code: Intentend伪代码:

  1. Loop through all slides 循环浏览所有幻灯片
  2. If you find a slide with a shape named "Content" then: 如果找到形状为“内容”的幻灯片,则:
  3. Copy the image located at "image-path" to the image placeholder "FeaturePic2" (for example). 将位于“图像路径”的图像复制到图像占位符“ FeaturePic2”(例如)。

I reduced the code to an example, which does not work. 我将代码简化为一个示例,该示例不起作用。 What the code does is: Copying the image-path-picture to each slide, even if there is no shape named "Content". 该代码的作用是:即使没有名为“ Content”的形状,也将图像路径图片复制到每张幻灯片。 Secondly it does not populate "FeaturePic2" (which is the second place holder on this slide), but populates the first placeholder it finds. 其次,它不会填充“ FeaturePic2”(这是此幻灯片上的第二个占位符),而是填充找到的第一个占位符。

'<<BEGIN>>
On Error Resume Next 
template_path = "C:\test\test.pptx"
image_path = "C:\test\test.jpg"
Set pres_template = pptApp.Presentations.Open(template_path)


For Each slide In pres_template.Slides

       If Shape.Name = ("Content") Then
        slide.Shapes.Placeholders("FeaturePic2").Select msoTrue
        slide.Shapes.AddPicture Filename:=image_path, LinkToFile:=False, SaveWithDocument:=True, Left:=10, Top:=10
       End If

Next slide

'<<END>>

Additional question which might be helpful, too: How can a place holder with a certain name be deleted? 另一个可能也有帮助的问题:如何删除具有特定名称的占位符?

Kind regards and thanks in advance 亲切的问候和预先的感谢

The changes below should help with getting the correct picture, assuming that there IS a picture named "Content": 假设存在名为“内容”的图片,则以下更改应有助于获取正确的图片:

On Error Resume Next 
template_path = "C:\test\test.pptx"
image_path = "C:\test\test.jpg"
Set pres_template = pptApp.Presentations.Open(template_path)

' Always DIM your variables and
' Never use reserved names as variable names 
' 
Dim oSl as slide
Dim oSh as shape

For Each oSl In pres_template.Slides
  For each oSh in oSl.Shapes

       If oSh.Name = ("Content") Then
        oSl.Shapes.Placeholders("FeaturePic2").Select msoTrue
        oSl.Shapes.AddPicture Filename:=image_path, LinkToFile:=False, SaveWithDocument:=True, Left:=10, Top:=10
       End If

Next ' slide

Next, is there some specific reason for putting the picture into a placeholder rather than just adding it as a new picture shape? 接下来,是否有一些特定原因需要将图片放入占位符,而不仅仅是将其添加为新的图片形状? It'd be a great deal simpler/more reliable. 更简单/更可靠。

If you MUST use a placeholder, you'll probably have to put dummy text or other content into any placeholders you don't want the picture to drop into randomly, then remove the dummy text later. 如果必须使用占位符,则可能必须将虚拟文本或其他内容放入您不希望图片随机插入的任何占位符中,然后再删除虚拟文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM