Master Template Help Needed

Hi,

I am using the following code a to create a presentation from a pot template and then add new slides from different presentations. The master template has 4 title/slide masters in it and when it copys over the template it only copies the first title/slide master. Is their a way to copy over all 4 templates?

I need to add new slides from different presentations but it keeps the original slide master template and I need all the slides added to use the company master. Can you replace the master on the added slides so the are all the same?

I would very much appreciate it if you could help me with my code because I am stuck....Thanks

'Master Template (4 Title/Slide Master Combinations)
Dim Template = New Presentation(MapPath(".") + "\\Template3.pot")

'Destination File
Dim DestinationPPT = New Presentation(MapPath(".") + \\Blueprint3.ppt)
Dim DestinationSlide = DestinationPPT.GetSlideByPosition(1)
Dim Sorted As SortedList = New SortedList()

'Clone Template
Template.CloneSlide(Template.GetSlideByPosition(1), 1, DestinationPPT, Sorted)

'Remove First Slide
DestinationPPT.Slides.Remove(DestinationSlide)
DestinationPPT.DeleteUnusedMasters()

'Clone Slide From New Presentation
Dim ImportPPT = New Presentation(MapPath(".") + \\hot2.ppt)
Dim slide As Slide
slide = ImportPPT.GetSlideByPosition(1)
ImportPPT.CloneSlide(slide, DestinationPPT.Slides.LastSlidePosition + 1, DestinationPPT, Sorted)

'Build Browser Info
Me.Response.ContentType = "application/vnd.ms-powerpoint"
Me.Response.AppendHeader("Content-Disposition", "attachment; filename=result.ppt")
Me.Response.Flush()
Dim st As System.IO.Stream = Me.Response.OutputStream

'Send The File

DestinationPPT.Write(st)
Me.Response.End()

Dear AKennard,


Use Slide.ChangeMaster method to change the master of any normal slide.


Whenever you clone normal slide from other presentation, its masters are also copied to new presentation.


You should save the master id of the newly clonned slide before deleting it. e.g


Dim newlyClonedSlide as Slide

newlyClonedSlide=Template.CloneSlide(Template.GetSlideByPosition(1), 1, DestinationPPT, Sorted)


Dim mstrID as Integer

mstrID=newlyClonedSlide.MasterID


Then you should find out master slide from your destination presentation using Presentation.GetSlideById method


Slide mstrSlide as Slide

mstrSlide=DestinationPPT.GetSlideById(mstrID)


And finally change the master slide of any of the normal slide of your destination presentation


Slide candidateNormalSlide=DestinationPPT.GetSlideByPosition(1)

candidateNormalSlide.ChangeMaster(mstrSlide, true)

Thanks for the update. I have updated the code to reflect your changes and it is almost working. I am using the demo codebase to allow us to evaluate if the product fits our needs.

We are almost there but the newly added slides is using the title master and I need it to use the slide master. Can you help?

The code is not allowing me to use candidateNormalSlide.ChangeMaster(mstrSlide,true)...this is probally because of the demo code.

'Master Template (4 Title/Slide Master Combinations)

Dim Template = New Presentation(MapPath(".") + "\\Template.pot")

'Destination File

Dim DestinationPPT = New Presentation(MapPath(".") + "\\Blueprint.ppt")

Dim DestinationSlide = DestinationPPT.GetSlideByPosition(1)

Dim Sorted As SortedList = New SortedList()

'Updated Code

Dim newlyClonedSlide As Slide

newlyClonedSlide = Template.CloneSlide(Template.GetSlideByPosition(1), 1, DestinationPPT, Sorted)

Dim mstrID As Decimal

mstrID = newlyClonedSlide.MasterId

Dim mstrSlide As Slide

mstrSlide = DestinationPPT.GetSlideById(mstrID)

'Remove First Slide

DestinationPPT.Slides.Remove(DestinationSlide)

DestinationPPT.DeleteUnusedMasters()

'Clone Slide From New Presentation

Dim ImportPPT = New Presentation(MapPath(".") + "\\hot.ppt")

Dim slide As Slide

slide = ImportPPT.GetSlideByPosition(1)

ImportPPT.CloneSlide(slide, DestinationPPT.Slides.LastSlidePosition + 1, DestinationPPT, Sorted)

'Change Master

Dim candidateNormalSlide = DestinationPPT.GetSlideByPosition(DestinationPPT.Slides.LastSlidePosition)

candidateNormalSlide.ChangeMaster(mstrSlide)

'Build Browser Info

Me.Response.ContentType = "application/vnd.ms-powerpoint"

Me.Response.AppendHeader("Content-Disposition", "attachment; filename=result.ppt")

Me.Response.Flush()

Dim st As System.IO.Stream = Me.Response.OutputStream

'Send The File

DestinationPPT.Write(st)

Me.Response.End()

Thanks for you help.

FYI: Actually, title master is found in Presentation.Slides collectio along with normal slides; however its slide id is greater or equal to 0x8000,0000 (hex)

On the other hand, slide master is found in Presetation.Masters collection and its slide id is also greater or equal to 0x8000,0000 (hex)

So you need to add one check, if newlyClonedSlide.MasterID is one of Presentation.Masters[idx].SlideID, then type cast the mstrSlide into MainMaster , before passing it as a parameter to Slide.ChangeMaster method

e.g

Slide mstrSlide as Slide
mstrSlide = DestinationPPT.GetSlideById(mstrID)

If (mstrID Is one of the Presentation.Masters[idx].SlideID) Then
    MainMaster MasterSlide = mstrSlide
    Slide candidateNormalSlide = DestinationPPT.GetSlideByPosition(1)
    candidateNormalSlide.ChangeMaster(MasterSlide, True)
End If

I hope you understand my point.

Hi,

Please could you look at me code and tell me where I am going wrong. I have attached the results.ppt file so you can see what I am ending up with. I am importing 3 slides from an external presentation and want the new slides to have their existing slide template replaced with the slide template in the destination presentation. The import is working but the imported slides are taking on the front page template and not the slide template. The slide template is their because if I add a new slide then the correct slide template is shown. The codes I am using is as follows.

I have read your manual and just cannot find out where I am going wrong. I have a huge deadline with regard to this project and would very much appreciate your help here.

'Master Template (4 Title/Slide Master Combinations)

Dim Template = New Presentation(MapPath(".") + "\\Template.pot")

'Destination File

Dim DestinationPPT = New Presentation(MapPath(".") + "\\Blueprint.ppt")

Dim DestinationSlide = DestinationPPT.GetSlideByPosition(1)

Dim Sorted As SortedList = New SortedList()

'Clone Slide and Store Master ID of New Slide

Dim newlyClonedSlide As Slide

newlyClonedSlide = Template.CloneSlide(Template.GetSlideByPosition(1), 1, DestinationPPT, Sorted)

'Remove First Slide

DestinationPPT.Slides.Remove(DestinationSlide)

DestinationPPT.DeleteUnusedMasters()

Dim mstrID As Decimal

mstrID = newlyClonedSlide.MasterId

'Calculate Master ID

Dim mstrSlide As Slide

mstrSlide = DestinationPPT.GetSlideById(mstrID)

'Clone Slide From New Presentation

Dim ImportPPT = New Presentation(MapPath(".") + "\\hot.ppt")

Dim slide As Slide

Dim Outer As Integer

For Outer = 1 To 3

slide = ImportPPT.GetSlideByPosition(Outer)

ImportPPT.CloneSlide(slide, DestinationPPT.Slides.LastSlidePosition + 1, DestinationPPT, Sorted)

'Change Master

Dim candidateNormalSlide = DestinationPPT.GetSlideByPosition(DestinationPPT.Slides.LastSlidePosition)

candidateNormalSlide.ChangeMaster(mstrSlide)

Next

'Build Browser Info

Me.Response.ContentType = "application/vnd.ms-powerpoint"

Me.Response.AppendHeader("Content-Disposition", "attachment; filename=result.ppt")

Me.Response.Flush()

Dim st As System.IO.Stream = Me.Response.OutputStream

'Update Presentation Info

DestinationPPT.Author = "Name"

DestinationPPT.Title = "Presentation Title"

DestinationPPT.Company = "Company"

DestinationPPT.Comments = "Modified Presentation Properties"

DestinationPPT.Subject = "Presentation Properties"

'Send The File

DestinationPPT.Write(st)

Me.Response.End()

Kind regards

Alan

Dear Alan,

Please change these two lines of code

'Change Master
Dim candidateNormalSlide = DestinationPPT.GetSlideByPosition(DestinationPPT.Slides.LastSlidePosition)
candidateNormalSlide.ChangeMaster(mstrSlide)

as this

ChangeMaster2(DestinationPPT, DestinationPPT.Slides.LastSlidePosition, mstrID)

And the code for ChangeMaster2 method Is given below

Sub ChangeMaster2(ByVal srcPres As Presentation, ByVal slideNo As Integer, ByVal masterID As UInteger)
    Dim sld As Slide
    sld = srcPres.GetSlideByPosition(slideNo)

    'if no slide is at this position then exit
    If sld Is Nothing Then
        Exit Sub
    End If

    Dim masterSld As Slide
    masterSld = srcPres.GetSlideById(masterID)

    'if no master slide with this id, then exit
    If masterSld Is Nothing Then
        Exit Sub
    End If

    'Master slide is found in Presentation.Masters collection
    'check if master is found in Presentation.Masters collection
    'if it is found then change master by passing MainMaster object
    'to Slide.ChangeMaster method
    Dim i As Integer
    For i = 0 To srcPres.Masters.Count - 1
        If srcPres.Masters.Item(i).SlideId = masterID Then
            sld.ChangeMaster(srcPres.Masters.Item(i), True)
            Exit Sub
        End If
    Next

    'Otherwise, the master is actually a title master
    'so proceed as below
    sld.ChangeMaster(masterSld, True)
End Sub

Thanks for you help so far but the output is still the same. I have attached all of the related files and code pages so you can see the test project in its entirity. The aim is to get the slide template (mouse background) added to the slides that are being cloned in from hot.ppt.

I have just convinced my boss to pay for this so we are awaiting our licence code.

I have promised him it will work so it would be great if you could get it to work for me.

Thanks in advance

Alan

Dear Alan,


The template.pot has four title/slides masters in it but there is only one

normal slide that follows the first slide master.


Your template should have 8 normal slides, where each one follows unique master

either slide/title.


I mean, first normal slide should have first slide master ....

2nd should have 1st title master

3rd should have 2nd slide master

4th should have 2nd title master

5th should have 3rd slide master

6th should have 3rd title master

7th should have 4th slide master

8th should have 4th title master


Now when you will clone let say 3rd normal slide from your template.pot, Aspose.Slides will also copy the 2nd slide master in your destination presentation and so on.


You will then save the master slide ID of the newly cloned slide and then delete this extra slide, because its sole purpose was to copy master from template.pot to your destination presentation.


Then you can use my code and the method ChangeMaster2, I provided earlier.


If you still feel problem, then let me know about it.

After adding all the 8 slides, change this line


newlyClonedSlide = Template.CloneSlide(Template.GetSlideByPosition(1), 1, DestinationPPT, Sorted)


into this


newlyClonedSlide = Template.CloneSlide(Template.GetSlideByPosition(2), 1, DestinationPPT, Sorted)


Change is highlighted. And then run your code again


You will see, now all of normal slides in result.ppt follow the master of the second slide in you template.pot

Hi Shakeel,

I have tried updated the number from 1 to 2 and it is causing an error. How do you add all 8 slides from the template?

Sorry for me being such an pain but I am very new to this.

Thanks

Alan

Dear Alan,

I mean, you should add normal slides in your template.pot using MS-PowerPoint and change masters of each of them and save back template.pot and then run the code after doing modifications told by me

Thank you for you help. Finally it is working.