无需选择格式化图像

时间:2014-03-22 03:44:22

标签: powerpoint powerpoint-vba

我想对幻灯片中的图片执行各种格式化选项。

宏在我在幻灯片中选择的图像上运行,但我想在不选择图像的情况下运行宏。

以下是我当前正在操作图像的方式(在这种情况下将图像与幻灯片的水平中心对齐)和我正在寻找的代码片段有助于替换:

With ActiveWindow.Selection.ShapeRange
 .Align (msoAlignCenters), msoTrue
End With

到目前为止,这是整个代码体:

Sub TestCenterImage()
Dim osld As Slide
Dim oshp As Shape

For Each osld In ActivePresentation.Slides
 If osld.SlideIndex > 1 Then Exit Sub 'I don't know if I need this line
 For Each oshp In osld.Shapes
  If CheckIsPic(oshp) = True Then 'Making sure that we're only working with images
   With ActiveWindow.Selection.ShapeRange 'The portion of code I need help with
    .Align (msoAlignCenters), msoTrue
   End With
  End If
 Next oshp
Next osld
End Sub

Function CheckIsPic(oshp As Shape) As Boolean
If oshp.Type = msoPicture Then CheckIsPic = True
 If oshp.Type = msoPlaceholder Then
  If oshp.PlaceholderFormat.ContainedType = msoPicture Then CheckIsPic = True
End If
End Function

2 个答案:

答案 0 :(得分:0)

For Each osld In ActivePresentation.Slides
 If osld.SlideIndex > 1 Then Exit Sub 'I don't know if I need this line

'如果您保留该行,您的代码将只触及演示文稿中的第一张幻灯片。 '如果这就是你想要的,那很好。否则,删除它和匹配的结束如果在下面。

 For Each oshp In osld.Shapes
  If CheckIsPic(oshp) = True Then 'Making sure that we're only working with images

'使用ActiveWindow.Selection.ShapeRange'我需要帮助的部分代码 '代替: 用oshp

    .Align (msoAlignCenters), msoTrue
   End With
  End If
 Next oshp
Next osld
End Sub

答案 1 :(得分:0)

请尝试这种方式:

Sub TestCenterImage()
Dim osld As Slide
Dim oShp As Shape

For Each osld In ActivePresentation.Slides
 'If osld.SlideIndex > 1 Then Exit Sub 'I don't know if I need this line
 For Each oShp In osld.Shapes
  If CheckIsPic(oShp) = True Then 'Making sure that we're only working with images
    CenterOnSlide oShp
   'End With
  End If
 Next oShp
Next osld
End Sub

Function CheckIsPic(oShp As Shape) As Boolean
If oShp.Type = msoPicture Then CheckIsPic = True
 If oShp.Type = msoPlaceholder Then
  If oShp.PlaceholderFormat.ContainedType = msoPicture Then CheckIsPic = True
End If
End Function

Sub CenterOnSlide(oShp As Shape)
    Dim sngSlideWidth As Single
    Dim sngSlideHeight As Single

    sngSlideWidth = ActivePresentation.PageSetup.SlideWidth
    sngSlideHeight = ActivePresentation.PageSetup.SlideHeight

    oShp.Left = sngSlideWidth / 2 - oShp.Width / 2
    oShp.Top = sngSlideHeight / 2 - oShp.Height / 2


End Sub