2012-02-05 70 views
5

我正在一个项目上工作。在那我做了一个自定义的主题,其中包括一个主幻灯片和可能的布局。 所以基本上我想将特定的布局应用于特定的幻灯片。那么有没有什么办法可以通过编程来完成。 像:如何使用vba在PowerPoint中应用特定的布局?

activepresentation.Slides(1).Layout = “layoutname”

我知道上面的代码是错误的,但我想这样的事情由它的名字来称呼特定布局。为了您的信息我的布局名称是“标题没有客户端标识”。

由于

回答

4

ActivePresentation.Slides(1).CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(x)的

其中x是索引到布局集合,表示自定义布局。

与PPT OM中的大多数其他此类集合不同,这一个似乎无法接受索引或名称。它必须是一个索引。

如果您需要使用该名称,请编写一个迭代CustomLayouts集合的函数,直到它找到您想要的名称并返回索引。

+0

嘿史蒂夫,其实我解决了我的问题。你是正确的功能是需要它的。我写了它。谢谢你的评论。 – 2012-02-05 19:08:53

+0

小心分享你的功能,@PratikGujarathi?我知道这很简单,但它会为未来的观众节省一些时间。 – sfarbota 2015-02-21 19:51:28

3

使用下面的代码

Sub ApplyLayoutByIndex() 

    Dim sld As Slide 
    Dim shp As Shape 
    Dim xName As String 
    Set sld = Application.ActiveWindow.View.Slide 
    Dim xIndex As Integer 

    xName = "A final slide" 

    xIndex = getLayoutIndexByName(xName) 

    If xIndex = 0 Then 
    MsgBox "The layout name" & xName & "not found. Check the name of the layout", vbOKOnly 
    Exit Sub 
    End If 

    sld.CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(xIndex) 

    End Sub 

    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 
      If .Item(i).Name = xName Then 
      getLayoutIndexByName = i 
      Exit Function 
      End If 
     Next 
    End With 

    End Function 

谢谢!

相关问题