如何将powerpoint宏应用于我当前正在查看的幻灯片?

时间:2012-09-28 15:02:05

标签: vb.net macros powerpoint

我正在使用powerpoint中的VB工作,我正在尝试编写一个简单的宏,只调整我正在编辑的幻灯片上的任何图像。我已经成功编写了大小和居中图像的代码,但它只适用于我演示文稿中的第一张幻灯片,即使我正在查看和编辑不同的幻灯片。如何将代码应用于我正在查看的幻灯片?

代码如下:

Dim shp As Shape
Dim sld as Slide

Dim x As Integer
Dim y As Integer

With ActivePresentation.PageSetup
x = .SlideWidth / 2
y = .SlideHeight / 2
End With


For Each sld In ActiveWindow.Selection.SlideRange
For Each shp In sld.Shapes

If shp.Type = msoPicture Then
shp.Height = y * 2 

shp.Width = x * 2

shp.Left = x - (shp.Width / 2)
shp.Top = y - (shp.Height / 2)
End If

Next
Next


End Sub

非常感谢!

1 个答案:

答案 0 :(得分:0)

逐步执行代码,看看此行后会发生什么:

如果shp.Type = msoPicture那么

幻灯片1后执行以下行吗?我猜你有msoPlaceholder类型的形状而不是msoPicture,所以你的大小调整代码永远不会触发。

请改为尝试:

Dim shp As Shape
Dim sld As Slide

Dim x As Integer
Dim y As Integer

With ActivePresentation.PageSetup
x = .SlideWidth / 2
y = .SlideHeight / 2
End With


For Each sld In ActiveWindow.Selection.SlideRange
For Each shp In sld.Shapes

If shp.Type = msoPicture Then
shp.Height = y * 2

shp.Width = x * 2

shp.Left = x - (shp.Width / 2)
shp.Top = y - (shp.Height / 2)
End If

If shp.Type = msoPlaceholder Then
    If shp.PlaceholderFormat.ContainedType = msoPicture Then
        shp.Height = y * 2
        shp.Width = x * 2
        shp.Left = x - (shp.Width / 2)
        shp.Top = y - (shp.Height / 2)
    End If
End If

Next
Next