2011-12-14 45 views
0

我試圖讓PowerPoint加載圖像以在幻燈片更改時替換佔位符。 我有代碼工作,從本地驅動器或URL更改佔位符的圖像。但它不會在OnSlideShowPageChange()事件(提到here)上工作。由於沒有以前的VB/VBA經驗,我不知道爲什麼,因爲它沒有給出任何錯誤。我知道該事件被訪問,因爲如果我在其中放入MsgBox()-函數,它就會顯示。PowerPoint/VBA:如何在幻燈片加載時用圖像替換佔位符

ImageReplace代碼:

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# 

任何幫助表示讚賞

回答

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 

BTW,調試和異常似乎並不在OnSlideShowPageChange事件的支持。作爲一種簡單的方法,在每行代碼之後放置一個MsgBox以查看執行停止的位置。

+0

這工作,謝謝:) – Esa 2011-12-16 09:49:21