通过VBA(Powerpoint)添加的图片将插入到占位符中

时间:2016-03-03 14:51:34

标签: vba powerpoint-vba powerpoint-2010

我有一个Powerpoint 2010宏来将特定图片插入到活动幻灯片上的固定位置。

Dim oSlide As Slide
Dim oPicture As Shape

' Set oSlide to the active slide.
Set oSlide = Application.ActiveWindow.View.Slide

' Insert Image to Footer
 Set oPicture = oSlide.Shapes.AddPicture("PathToFile.png", _
  msoFalse, msoTrue, 630, 390, 15, 15)

' Move the picture to the center of the slide. Select it.
With ActivePresentation.PageSetup
  oPicture.Select
  oPicture.Name = "Dokumentverknüpfung"
End With

如果幻灯片上没有未使用的占位符,则此代码可以正常工作。 如果有占位符,则Picture会自动插入此占位符。

有没有办法告诉脚本避免使用占位符并只接受给定的坐标?

谢谢你, 延

2 个答案:

答案 0 :(得分:1)

没有办法明确告诉PowerPoint不要在图片中使用填充空占位符,但是可以通过确保没有空占位符来阻止它。如果在插入图片之前和之后调用子ProtectEmptyPlaceholders,则图片将作为新形状插入。

class ViewController: UIViewController {
  dynamic var myArray: NSArray!
}

答案 1 :(得分:0)

尝试将图片添加到新的临时幻灯片中,该幻灯片的空白布局不包含任何占位符。然后,剪切图片并将其粘贴到原始幻灯片上,然后删除临时幻灯片。

sldTemp = pres.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutBlank)

此方法的优点是(a)无论占位符是多用途的还是图片专用的,它都可以工作;在较小的程度上,(b)不需要循环遍历幻灯片上的所有形状两次。缺点是,在插入和删除临时幻灯片时,您会看到一些屏幕闪烁。

更新

这也许是避免插入空白幻灯片的更好方法。插入图片后,检查新插入的形状类型是否为占位符。如果是这样,那么我们需要Cut()图片并将其粘贴回幻灯片并调整形状的某些属性:

shpNew = Selection.SlideRange.Shapes.AddPicture(strImagePath, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue, x, y)
If shpNew.Type = Office.MsoShapeType.msoPlaceholder Then
    'PowerPoint put the picture into a placeholder, against our wishes
    shpNew.Cut()
    ppApp.ActiveWindow.View.Paste()
    shpNew = ppApp.ActiveWindow.Selection.ShapeRange(1)
    With shpNew
        With .PictureFormat
            .CropBottom = 0
            .CropLeft = 0
            .CropRight = 0
            .CropTop = 0
        End With
        .ScaleHeight(1, Office.MsoTriState.msoTrue)
        .ScaleWidth(1, Office.MsoTriState.msoTrue)
        .Top = y
        .Left = x
    End With
End If