如何使用 vba 在 powerpoint 中应用特定布局?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9147643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to apply particular layout in powerpoint using vba?
提问by Pratik Gujarathi
I am working on one project. In that I made one custom theme which includes one master slide and may layouts. so basically i want to apply particular layout to specific slides. So is there any way to do it by programmatically. like :
我正在做一个项目。我制作了一个自定义主题,其中包括一张母版幻灯片和可能的布局。所以基本上我想将特定的布局应用于特定的幻灯片。那么有没有办法通过编程来做到这一点。喜欢 :
activepresentation.Slides(1).Layout="layoutname"
activepresentation.Slides(1).Layout="layoutname"
I know above code is wrong but i want something like this to call particular layout by its name. for your information my layout name is "Title without Client Logo".
我知道上面的代码是错误的,但我想要这样的东西通过它的名字来调用特定的布局。为了您的信息,我的布局名称是“没有客户徽标的标题”。
Thanks
谢谢
回答by Steve Rindsberg
ActivePresentation.Slides(1).CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(x)
ActivePresentation.Slides(1).CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(x)
where x is the index into the layouts collection that represents your custom layout.
其中 x 是表示您的自定义布局的布局集合的索引。
Unlike most other such collections in the PPT OM, this one seems unable to accept either an index or a name. It must be an index.
与 PPT OM 中的大多数其他此类集合不同,这个集合似乎无法接受索引或名称。它必须是一个索引。
If you need to work with the name, write a function that iterates through the CustomLayouts collection until it finds the name you're after and returns the index.
如果您需要使用名称,请编写一个函数,该函数遍历 CustomLayouts 集合,直到找到您要查找的名称并返回索引。
回答by Николай Каретников
Use the following code
使用以下代码
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
Thanks!
谢谢!