PowerPoint/VBA:如何在加载幻灯片时用图像替换占位符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8500370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:42:19  来源:igfitidea点击:

PowerPoint/VBA: How to replace placeholder with image when slide loads

vbapowerpointpowerpoint-vba

提问by Esa

I trying to make PowerPoint load up images to replace placeholders everytime a slide changes. I have the code working, which changes the placeholders with images from local drive or url. But it wont work on OnSlideShowPageChange()event(mentioned here). With no prior experience on VB/VBA, I have no idea why, as it does not give any errors. I know the event is accessed because if I put a MsgBox()-function in it, it is displayed.

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

ImageReplace code:

图像替换代码:

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#

Any help is appreciated

任何帮助表示赞赏

回答by Paul B.

ActiveWindow is not accessible when in slide show view.

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

Try this instead

试试这个

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

BTW, debugging and exceptions do not seem to be supported in the OnSlideShowPageChange event. As an easy approach place a MsgBox after each line of code to see where the execution stops.

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