【问题标题】:PowerPoint/VBA: How to replace placeholder with image when slide loadsPowerPoint/VBA:幻灯片加载时如何用图像替换占位符
【发布时间】:2011-12-16 16:55:19
【问题描述】:

我试图让 PowerPoint 在每次幻灯片更改时加载图像以替换占位符。 我的代码正在运行,它使用来自本地驱动器或 url 的图像更改占位符。但它不适用于OnSlideShowPageChange() 事件(提到here)。由于没有 VB/VBA 方面的经验,我不知道为什么,因为它没有给出任何错误。我知道该事件已被访问,因为如果我将 MsgBox()-function 放入其中,它就会显示出来。

图片替换代码:

Dim strPicName As String
Dim shp As Shape
Dim sglShapeLeft As Single
Dim sglShapeTop As Single
Dim sglShapeHeight As Single
Dim sglShapeWidth As Single

'Get the name of the shape (image)
'Provided this is the only shape on the slide
'Since I don't think you can use the ME. keyword to reference an impage from Powerpoint VBA
'(Me.shape.Name)
For Each shp In ActiveWindow.Selection.SlideRange.Shapes
    strPicName = shp.Name
Next shp

'Select the Image
ActiveWindow.Selection.SlideRange.Shapes(strPicName).Select
'Get the Left and Top starting points and width and height
sglShapeLeft = ActiveWindow.Selection.SlideRange.Shapes(strPicName).Left
sglShapeTop = ActiveWindow.Selection.SlideRange.Shapes(strPicName).Top
sglShapeHeight = ActiveWindow.Selection.SlideRange.Shapes(strPicName).Height
sglShapeWidth = ActiveWindow.Selection.SlideRange.Shapes(strPicName).Width
'Delete the Image
ActiveWindow.Selection.ShapeRange.Delete
'Insert a new Image at the same starting points as the previous image
ActiveWindow.Selection.SlideRange.Shapes.AddPicture(FileName:="<picturePath/url>", LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=sglShapeLeft, Top:=sglShapeTop, Width:=sglShapeWidth, Height:=sglShapeHeight).Select

For Each shp In ActiveWindow.Selection.SlideRange.Shapes
    strPicName = shp.Name
Next shp

ActiveWindow.Selection.SlideRange.Shapes(strPicName).IncrementRotation 276#

感谢任何帮助

【问题讨论】:

    标签: vba powerpoint


    【解决方案1】:

    在幻灯片放映视图中无法访问 ActiveWindow。

    试试这个

    Dim sld As Slide
    Set sld = ActivePresentation.Slides _
        (ActivePresentation.SlideShowWindow.View _
        .CurrentShowPosition)
    
    Set shp = sld.Shapes(1)
    
    With shp
        sld.Shapes.AddPicture(FileName:="<picturePath/url>", LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height).IncrementRotation 276#
        .Delete
    End With
    

    顺便说一句,OnSlideShowPageChange 事件似乎不支持调试和异常。作为一种简单的方法,在每行代码之后放置一个 MsgBox 以查看执行停止的位置。

    【讨论】:

      猜你喜欢
      • 2019-06-11
      • 1970-01-01
      • 1970-01-01
      • 2014-12-27
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      相关资源
      最近更新 更多