简体   繁体   English

VBA PowerPoint 幻灯片设置自定义布局以刷新布局

[英]VBA PowerPoint Slides set custom layout to refresh the layout

I have created a script processing many slides and at the end, some slides seem to have glitches in their layout.我创建了一个脚本来处理许多幻灯片,最后,一些幻灯片的布局似乎有问题。 For example, slide numbers have moved on some slides but not on others.例如,幻灯片编号在某些幻灯片上移动了,但在其他幻灯片上没有移动。 It can be fixed manually by re-assigned the custom layout to the slide.可以通过将自定义布局重新分配给幻灯片来手动修复它。

How can I do this automatically?我怎样才能自动做到这一点?

I could just loop over all slides, find out it's custom layout and re-assign it.我可以遍历所有幻灯片,找出它的自定义布局并重新分配它。 But how?但是怎么做? This code seems to loop infinitely:这段代码似乎无限循环:

Dim sld As Slide
Dim layoutName As String
Dim layoutIndex As Integer

       Set sld = Application.ActiveWindow.View.Slide 
       layoutName = sld.CustomLayout.Name
       layoutIndex = getLayoutIndexByName(layoutName)
       ActivePresentation.Slides(y).CustomLayout = ActivePresentation.Designs(y).SlideMaster.CustomLayouts(layoutIndex) 


Function getLayoutIndexByName(xName As String) As Integer
   ActivePresentation.Designs(1).SlideMaster.CustomLayouts.Item (1)
   With ActivePresentation.Designs(1).SlideMaster.CustomLayouts
      For i = 1 To .Count
        Debug.Print ("inLoop Name: " + .Item(i).Name)
        If .Item(i).Name = xName Then
        getLayoutIndexByName = i
        Exit Function
        End If
   Next
   End With

End Function

To simply reapply the layout already assigned, you only need this:要简单地重新应用已分配的布局,您只需要:

ActivePresentation.Slides(y).CustomLayout = ActivePresentation.Slides(y).CustomLayout

Occasionally, that command doesn't work, then this workaround is worth a try:有时,该命令不起作用,那么这个解决方法值得一试:

DoEvents
Application.CommandBars.ExecuteMso ("SlideReset")
DoEvents

To apply a new layout, then you need to use something like this code, which is pretty similar to yours:要应用新布局,您需要使用类似于以下代码的代码,这与您的代码非常相似:

ActivePresentation.Slides(y).CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(GetLayoutIndexFromName("Text Page", ActivePresentation.Designs(1)))

My version of GetLayoutIndexFromName:我的 GetLayoutIndexFromName 版本:

Function GetLayoutIndexFromName(sLayoutName As String, oDes As Design) As Long
  Dim x As Long
  For x = 1 To oDes.SlideMaster.CustomLayouts.Count
    If oDes.SlideMaster.CustomLayouts(x).Name = sLayoutName Then
      GetLayoutIndexFromName = x
      Exit Function
    End If
  Next
End Function

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

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