Consulting

Results 1 to 4 of 4

Thread: Solved: Trouble referring to custom layouts by string (PowerPoint 2007)

  1. #1

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

    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:
    [vba]

    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("InternalDiv ider")
    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

    [/vba]

    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

  2. #2
    VBAX Contributor
    Joined
    May 2008
    Posts
    198
    Location
    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?

  3. #3
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    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.

    [VBA]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"[/VBA]
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  4. #4
    Thanks Cosmo and John

    Looping through the custom layout collection did the trick!

    [VBA]
    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
    [/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •