Vba 格式化图像而不选择

Vba 格式化图像而不选择,vba,powerpoint,Vba,Powerpoint,我想对幻灯片中的图像执行各种格式设置选项 宏在我在幻灯片中选择的图像上运行,但我希望在不选择图像的情况下运行宏 以下是我当前操作图像的方式(在本例中,将图像与幻灯片的水平中心对齐)以及我正在寻找帮助的代码片段: With ActiveWindow.Selection.ShapeRange .Align (msoAlignCenters), msoTrue End With 以下是到目前为止的整个代码体: Sub TestCenterImage() Dim osld As Slide Dim

我想对幻灯片中的图像执行各种格式设置选项

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

以下是我当前操作图像的方式(在本例中,将图像与幻灯片的水平中心对齐)以及我正在寻找帮助的代码片段:

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
'如果保留该行,您的代码将只触及演示文稿中的第一张幻灯片。 “如果那是你想要的,好吧。否则,删除它和匹配端(如果在下面)

 For Each oshp In osld.Shapes
  If CheckIsPic(oshp) = True Then 'Making sure that we're only working with images
'With ActiveWindow.Selection.ShapeRange'我需要帮助的代码部分 “相反: 与oshp合作

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

试着这样做:

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

嗨,史蒂夫,谢谢你抽出时间回答我的问题。我用您的建议更新了代码,返回了以下错误:“未找到方法或数据成员”,它指向
。Align
我如何更改代码以供参考,很可能是我的错误源:
对于ActivePresentation中的每个osld。如果osld.SlideIndex>1,则幻灯片中的每个oshp退出Sub。如果CheckIsPic(oshp)=True,则使用osld.oshp.Align(msoAlignCenters),如果下一个oshp下一个osld End Sub,则msoTrue End With End,但我有一个潜在的并发症。虽然这段代码确实有效且准确地允许我在幻灯片中居中显示图像,但我倾向于使用
With
命令(操作?),因为我希望在将来添加代码以进行其他格式更改,并且基于我的谷歌搜索,似乎(因为我是新手)使用
With
命令是一种可行的方法。我期待你的专业知识。非常感谢!!!您可以轻松修改CenterOnSlide例程以与oShp一起使用,并向形状添加更多格式,而不仅仅是将其居中;或者完全更改名称以反映要对形状执行的操作。形状或任何东西。使用With/End With比重复使用oShp.ThisProperty快一点。在这种情况下,速度更快,但不会让你注意到任何差异,但这不会造成伤害,而且可以节省打字时间。
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