Setting slide-master in PowerPoint & filling fields

User avatar
ErikJan
BronzeLounger
Posts: 1258
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Setting slide-master in PowerPoint & filling fields

Post by ErikJan »

I have a PowerPoint template with three different slide masters. Want to create slides from VBA using fields in Excel. Have two little problems... (regretfully, I cannot record sufficiently detailed enough code via a macro recorder as that does not really exist anymore. Alt- T M R, does a little I found but not enough)

(1) Depending on some fields in Excel, I need to select one of the three masters for each new slide. How do I do this in VBA? I believe I can name the masters, but I would need to select it from VBA (as one does with the 'Layout" button on the Ribbon)

(2) Once I have the right template selected for each slide, I need to fill in the fields. Now I can do this 'dirty' by presetting the contents of the fields in the master and doing a replace

Code: Select all

        For Each objShp In objSld.Shapes
            If objShp.HasTextFrame Then
                If objShp.TextFrame.HasText Then
                    objShp.TextFrame.TextRange.Replace "Placeholder field1", "my variable text"
                End If
            End If
        Next
I'd rather just 'enter' the text in the placeholder for each field. To do that, I need to be able to 'select' each field and then enter text into it... How do I do that?

User avatar
HansV
Administrator
Posts: 78608
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Setting slide-master in PowerPoint & filling fields

Post by HansV »

(1) As far as I can tell, you cannot change the master of a slide in VBA. The Master property is read-only.
Or did you mean the slide layout?

(2) You could use something like this:

Code: Select all

    With objSld.Shapes.Placeholders
        .Item(1).TextFrame.TextRange.Text = Range("A2")
        .Item(2).TextFrame.TextRange.Text = Range("B2")
    End With
Best wishes,
Hans

User avatar
ErikJan
BronzeLounger
Posts: 1258
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Setting slide-master in PowerPoint & filling fields

Post by ErikJan »

(1) yes, I mean 'apply one of the three slide master layouts to the current slide'

(2) Great, can I give "Item(1)" and "Item(2)" names, so I know for sure where my text goes?

User avatar
HansV
Administrator
Posts: 78608
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Setting slide-master in PowerPoint & filling fields

Post by HansV »

PowerPoint VBA is extremely frustrating.

(1) I don't know how to do that. You can set the Layout of a slide: see Slide.Layout Property.

(2) Probably, but PowerPoint is very stubborn in this respect, I can't get it to work reliably. The Title placeholder is always objSld.Shapes.Title, by the way.
Best wishes,
Hans

User avatar
ErikJan
BronzeLounger
Posts: 1258
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Setting slide-master in PowerPoint & filling fields

Post by ErikJan »

Found this http://skp.mvps.org/2007/ppt003.htm. Looks promising, I'll check it out