繁体   English   中英

使用 Excel VBA 删除多个 powerpoint 文件的未使用母版幻灯片

[英]Removing unused master slide of multiples powerpoint files using Excel VBA

你好 Stackoverflow 社区,

我希望从多个 powerpoint 演示文稿中删除未使用的 masterslides。 文件列表位于 excel 文件中。 我写了一个宏来打开每个 powerpoint 文件。 我发现一个在 powerpoint VBA 中使用的宏删除了未使用的 masterslide,但是当我将它包含在我的 Excel 宏中时它不起作用......而且我无法保存和关闭每个 pwp 文件。

循环文件的宏:


Dim myPresentation As Object
Dim PowerPointApp As Object
Set myPresentation = CreateObject("Powerpoint.application")

'Find last row of path files list
lastRow = Cells(Rows.Count, "A").End(xlUp).Row

'Looping through files
For i = 1 To lastRow
    
    'Defines pwp file to open
    DestinationPPT = Cells(i, "A")
    'opens pwp file
    myPresentation.presentations.Open DestinationPPT
    myPresentation.Visible = True
    
    'Then I would like to : remove unused master slide, save, close
    
Next i

End Sub 

直接在 pwp 中使用时起作用的宏:

Sub SlideMasterCleanup()

Dim k As Integer
Dim n As Integer
Dim oPres As Presentation
Set oPres = ActivePresentation
On Error Resume Next
With oPres
    For k = 1 To .Designs.Count
        For n = .Designs(k).SlideMaster.CustomLayouts.Count To 1 Step -1
            .Designs(k).SlideMaster.CustomLayouts(n).Delete
        Next
    Next k
End With

End Sub

我该怎么做才能:

  • 成功删除我的 Excel 宏中的 masterslide
  • 在进入下一个之前保存并关闭每个 pwp

非常感谢

这是修改代码的第一步。 试试看; 如果它有效,那就太好了。 如果没有,请告诉我们出了什么问题,以及在哪一行代码。 仅在您的演示文稿副本上使用它。 我看不到您在哪里编写了任何确定是否使用布局的方法。

Option Explicit

Sub Main()

Dim myPresentation As Object
Dim PowerPointApp As Object
Set PowerPointApp = CreateObject("Powerpoint.application")

'Find last row of path files list
LastRow = Cells(Rows.Count, "A").End(xlUp).Row

'Looping through files
For i = 1 To LastRow
    
    'Defines pwp file to open
    DestinationPPT = Cells(i, "A")
    'opens pwp file
    Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)
    myPresentation.Visible = True
    
    'Then I would like to : remove unused master slide, save, close
    Call SlideMasterCleanup(myPresentation)
    
Next i

End Sub

Sub SlideMasterCleanup(oPres As Presentation)

Dim k As Integer
Dim n As Integer

On Error Resume Next
With oPres
    For k = 1 To .Designs.Count
        For n = .Designs(k).SlideMaster.CustomLayouts.Count To 1 Step -1
            .Designs(k).SlideMaster.CustomLayouts(n).Delete
        Next
    Next k
End With

oPres.Save
oPres.Close

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM