人气 7186

[PPT技巧] 如何同时删除PPT报告中所有页面的同样的图片呢? [复制链接]

yuano8o8 2023-2-22 20:16:09
使用VBA

① 首先打开 PPT 中的 VBA 编辑器,并在新出现的编辑器窗口中点击「插入」→「模块」:
删除图片.png

在 Windows 版 PPT 中打开 Visual Basic 编辑器,并插入一个模块

删除图片.png
② 在新插入的模块的编辑器中输入以下代码,然后点击顶部菜单栏中的三角按钮来运行(运行之前需要在 PPT 窗口内选中一个或多个需要统一位置的形状):


选中的那些形状会被作为参考,之后其他页中与它们名字相同且类型/位置/大小相同的形状,都会被删除。

【注意】:这里的名字是指选中某个形状后,在「选择窗格」的形状列表中对应被选中项的名字


删除图片.png

③ 运行之后,回到 PPT 窗口,这时当前 PPT 内所有与被选中的形状有相同名字和类型/位置/大小的形状,都会被删除。演示效果如下:

   示例中只选取了一个形状(名字为“logo”,类型为图片),但其实也支持选中多个不同需要删除的形状(它们将被分别处理)


删除图片.png

上面用到的 VBA 代码如下:

如果只需要匹配形状的大小和位置,则按需求删除下面代码中的 If 条件即可,即删除 shp.Type = selectedShp.Type _ 和 And shp.Name = selectedShp.Name _,并将 And shp.Left = selectedShp.Left _ 最前面的 And 删除。

Sub DeleteShapesBySelection()
    Dim sld As Slide
    Dim shp, selectedShp As Shape
    Dim slideIndex As Long
    Dim count, idx, shpCount As Long

    If ActiveWindow.Selection.Type = ppSelectionShapes Or ActiveWindow.Selection.Type = ppSelectionText Then
        ' A shape is selected or a textbox is focused (text is selected)
        count = 0
        slideIndex = ActiveWindow.Selection.SlideRange.SlideIndex
        For Each selectedShp In ActiveWindow.Selection.ShapeRange
            For Each sld In ActivePresentation.Slides
                If sld.SlideIndex <> slideIndex Then
                    shpCount = sld.Shapes.count
                    For idx = shpCount To 1 Step -1
                        Set shp = sld.Shapes(idx)
                        If ( _
                            shp.Type = selectedShp.Type _
                            And shp.Name = selectedShp.Name _
                            And shp.Left = selectedShp.Left _
                            And shp.Top = selectedShp.Top _
                            And shp.Width = selectedShp.Width _
                            And shp.Height = selectedShp.Height _
                        ) Then
                            shp.Delete
                            count = count + 1
                        End If
                    Next idx
                End If
            Next sld
            selectedShp.Delete
            count = count + 1
        Next selectedShp
        MsgBox "共删除了" & count & "个形状。"
    Else
        MsgBox "未发现任何选中的形状或文本框", vbExclamation, "No Shape Found"
    End If
End Sub



您需要登录后才可以回帖 登录 | 立即注册

QQ|手机版|精益人 ( 沪ICP备19004111号-1 )

GMT+8, 2024-5-17 19:02 , Processed in 0.268805 second(s), 26 queries .

Powered by Lean.ren X3.5 Licensed  © 2001-2030 LEAN.REN