PDA

View Full Version : Solved: Trouble referring to custom layouts by string (PowerPoint 2007)



nickgill
09-18-2009, 10:28 AM
Hi

I'm trying to use VBA set the layout of the active slide (or any slide) to a custom layout.

I can achieve this by referring to the index number but can't get it to work if I try and refer to the name of the layout.

For example:


Dim oSld As Slide
Set oSld = ActivePresentation.Slides(1)
Dim oLayout As CustomLayout


'This code will work
Set oLayout = ActivePresentation.Designs("Master").SlideMaster.CustomLayouts(3)
oSld.CustomLayout = oLayout

'But this code will not work
Set oLayout = ActivePresentation.Designs("Master").SlideMaster.CustomLayouts("InternalDivider")
oSld.CustomLayout = oLayout

'This code does not work either
Dim strLayout As String
strLayout = ActivePresentation.Designs("Master").SlideMaster.CustomLayouts(3).Name

Set oLayout = ActivePresentation.Designs("Master").SlideMaster.CustomLayouts(strLayout)
oSld.CustomLayout = oLayout



For the two layouts that don't work I keep getting the error message "CustomLayouts (unknown member): Bad argument type. Expected collection index (string or integer)."

Has anyone been able to refer to a customlayout by string?

I would like to refer to the CustomLayouts by name in case the end user decides to rearrange the order of layouts or delete my layout (in which case the error handling will tell them the layout doesn't exist and to reapply the template).

Thanks

Nick

Cosmo
09-18-2009, 01:20 PM
I haven't tried to target a custom layout by name, but if it isn't working, why not try looping through the customLayout collection for ActivePresentation.Designs("Master").SlideMaster and comparing the name against the one you are looking for?

Also, are you sure the custom layout is a member of the design "Master"? Are there multiple masters in the presentation, and could the CustomLayout be a member of a different master?

John Wilson
09-18-2009, 10:52 PM
I don't think you can reference custom layouts by name (maybe a bug?)

As Cosmo suggests you can loop and jump out of the loop if you find a match.

Dim olay As CustomLayout
For Each olay In ActivePresentation.SlideMaster.CustomLayouts
If olay.Name = "MyLayout" Then Exit For
Next
Err.Clear
On Error Resume Next
MsgBox olay.Name & " FOUND"
If Err > 0 Then MsgBox "ERROR"

nickgill
09-21-2009, 01:44 AM
Thanks Cosmo and John

Looping through the custom layout collection did the trick!


Dim oSld As Slide
Set oSld = ActivePresentation.Slides(1)
Dim oLayout As CustomLayout
For Each oLayout In ActivePresentation.Designs("Master").SlideMaster.CustomLayouts
If oLayout.Name = "Internal Divider" Then
oSld.CustomLayout = oLayout
Exit For
End If
Next