VBA for merging 2 PPTX files alternatively

sksanjeev786

Well-known Member
Joined
Aug 5, 2020
Messages
883
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi Team,

I have 2 PPTX files and each PPTX file has almost 100+ slides so i would like to merge them alternatively

i.e Amount 2 PPT X File "A"PPTX already has 100 slides and "B"PPTX also have 100 Slides.

"A" PPTX slides are the first points and "B" PPTX slides are the continuation of A but the current is separately

so i would like to merge all slides in "A" Consider as "A1 as page 1 and B1 as page 1 from "B" file (ie. A1,B1,A2,B2,A3,B3,A4,B4 etc)

So overall it will be 200 pages alternatively.

Can anyone help me with this?

Regards
Sanjeev
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi Sanjeev,

I've been working on a solution for you. Please hang tight, and I will get you something soon, ok?

Thanks,
Chris
 
Upvote 0
Hello again,

I have two possible solutions for you, and you can choose which way you want to go based on your requirements.

For my first test, I created two PPTX files and saved them to a temporary folder on my desktop. We have A.pptx and B.pptx.

I then opened up a new PP file and added the following code to it. Make sure to change the paths to both of your PPTX files.
VBA Code:
Sub MergeAndAlternateSlides()
    Dim sourcePresA As Presentation
    Dim sourcePresB As Presentation
    Dim targetPres As Presentation
    Dim sourcePathA As String
    Dim sourcePathB As String
    Dim sourceSlide As Slide
    Dim targetSlide As Slide
    Dim i As Long
    Dim targetIndex As Long

    ' Set the paths to your A.pptx and B.pptx files
    sourcePathA = "C:\path\to\A.pptx"
    sourcePathB = "C:\path\to\B.pptx"

    ' Open the source presentations
    Set sourcePresA = Presentations.Open(sourcePathA)
    Set sourcePresB = Presentations.Open(sourcePathB)

    ' Create a new presentation to hold the merged slides
    Set targetPres = Presentations.Add

    ' Merge and alternate slides
    For i = 1 To sourcePresA.Slides.Count
        ' Copy slide from A.pptx
        sourcePresA.Slides(i).Copy
        targetIndex = (i - 1) * 2 + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.Shapes.Title.TextFrame.TextRange.Text = "A" & i

        ' Copy slide from B.pptx
        sourcePresB.Slides(i).Copy
        targetIndex = targetIndex + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.Shapes.Title.TextFrame.TextRange.Text = "B" & i
    Next i

    ' Close the source presentations without saving
    sourcePresA.Close
    sourcePresB.Close
End Sub

Hit the F5 button to run the code, and this is what pops up.



test1_copy.jpg


You can see that each slide is alternated like you requested, but it does so with a text label added to each slide. You may have to zoom in to see them.






Another option is to use the "notes" section of each slide. After running this script, the labels (A1, B1, A2, B2, etc.) will be added to the "Notes" section of each slide in the merged presentation. To view these labels while editing your presentation, click on the "Notes" button in the status bar at the bottom of the PowerPoint window or press Ctrl + Shift + H to show the "Notes" pane. The labels will not be visible when presenting the slides.


VBA Code:
Sub MergeAndAlternateSlides()
    Dim sourcePresA As Presentation
    Dim sourcePresB As Presentation
    Dim targetPres As Presentation
    Dim sourcePathA As String
    Dim sourcePathB As String
    Dim sourceSlide As Slide
    Dim targetSlide As Slide
    Dim i As Long
    Dim targetIndex As Long

    ' Set the paths to your A.pptx and B.pptx files
    sourcePathA = "C:\path\to\A.pptx"
    sourcePathB = "C:\path\to\B.pptx"

    ' Open the source presentations
    Set sourcePresA = Presentations.Open(sourcePathA)
    Set sourcePresB = Presentations.Open(sourcePathB)

    ' Create a new presentation to hold the merged slides
    Set targetPres = Presentations.Add

    ' Merge and alternate slides
    For i = 1 To sourcePresA.Slides.Count
        ' Copy slide from A.pptx
        sourcePresA.Slides(i).Copy
        targetIndex = (i - 1) * 2 + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.NotesPage.Shapes.Placeholders(2).TextFrame.TextRange.Text = "A" & i

        ' Copy slide from B.pptx
        sourcePresB.Slides(i).Copy
        targetIndex = targetIndex + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.NotesPage.Shapes.Placeholders(2).TextFrame.TextRange.Text = "B" & i
    Next i

    ' Close the source presentations without saving
    sourcePresA.Close
    sourcePresB.Close
End Sub


test2 copy.jpg


You'll see in the second picture, that no additional text was added to the slides, but the slide designations appear in the Notes at the bottom of the screen. There is no way that I know of to physically change the labels on the far left side of the slide bar. PowerPoint manually gives each slide a sequential number (1, 2, 3, 4, 5, etc.).


Hope this is what you're looking for. If not, let me know, and I can try something else.

Chris
 
Upvote 1
Solution
Hello again,

I have two possible solutions for you, and you can choose which way you want to go based on your requirements.

For my first test, I created two PPTX files and saved them to a temporary folder on my desktop. We have A.pptx and B.pptx.

I then opened up a new PP file and added the following code to it. Make sure to change the paths to both of your PPTX files.
VBA Code:
Sub MergeAndAlternateSlides()
    Dim sourcePresA As Presentation
    Dim sourcePresB As Presentation
    Dim targetPres As Presentation
    Dim sourcePathA As String
    Dim sourcePathB As String
    Dim sourceSlide As Slide
    Dim targetSlide As Slide
    Dim i As Long
    Dim targetIndex As Long

    ' Set the paths to your A.pptx and B.pptx files
    sourcePathA = "C:\path\to\A.pptx"
    sourcePathB = "C:\path\to\B.pptx"

    ' Open the source presentations
    Set sourcePresA = Presentations.Open(sourcePathA)
    Set sourcePresB = Presentations.Open(sourcePathB)

    ' Create a new presentation to hold the merged slides
    Set targetPres = Presentations.Add

    ' Merge and alternate slides
    For i = 1 To sourcePresA.Slides.Count
        ' Copy slide from A.pptx
        sourcePresA.Slides(i).Copy
        targetIndex = (i - 1) * 2 + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.Shapes.Title.TextFrame.TextRange.Text = "A" & i

        ' Copy slide from B.pptx
        sourcePresB.Slides(i).Copy
        targetIndex = targetIndex + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.Shapes.Title.TextFrame.TextRange.Text = "B" & i
    Next i

    ' Close the source presentations without saving
    sourcePresA.Close
    sourcePresB.Close
End Sub

Hit the F5 button to run the code, and this is what pops up.



View attachment 87866


You can see that each slide is alternated like you requested, but it does so with a text label added to each slide. You may have to zoom in to see them.






Another option is to use the "notes" section of each slide. After running this script, the labels (A1, B1, A2, B2, etc.) will be added to the "Notes" section of each slide in the merged presentation. To view these labels while editing your presentation, click on the "Notes" button in the status bar at the bottom of the PowerPoint window or press Ctrl + Shift + H to show the "Notes" pane. The labels will not be visible when presenting the slides.


VBA Code:
Sub MergeAndAlternateSlides()
    Dim sourcePresA As Presentation
    Dim sourcePresB As Presentation
    Dim targetPres As Presentation
    Dim sourcePathA As String
    Dim sourcePathB As String
    Dim sourceSlide As Slide
    Dim targetSlide As Slide
    Dim i As Long
    Dim targetIndex As Long

    ' Set the paths to your A.pptx and B.pptx files
    sourcePathA = "C:\path\to\A.pptx"
    sourcePathB = "C:\path\to\B.pptx"

    ' Open the source presentations
    Set sourcePresA = Presentations.Open(sourcePathA)
    Set sourcePresB = Presentations.Open(sourcePathB)

    ' Create a new presentation to hold the merged slides
    Set targetPres = Presentations.Add

    ' Merge and alternate slides
    For i = 1 To sourcePresA.Slides.Count
        ' Copy slide from A.pptx
        sourcePresA.Slides(i).Copy
        targetIndex = (i - 1) * 2 + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.NotesPage.Shapes.Placeholders(2).TextFrame.TextRange.Text = "A" & i

        ' Copy slide from B.pptx
        sourcePresB.Slides(i).Copy
        targetIndex = targetIndex + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.NotesPage.Shapes.Placeholders(2).TextFrame.TextRange.Text = "B" & i
    Next i

    ' Close the source presentations without saving
    sourcePresA.Close
    sourcePresB.Close
End Sub


View attachment 87867


You'll see in the second picture, that no additional text was added to the slides, but the slide designations appear in the Notes at the bottom of the screen. There is no way that I know of to physically change the labels on the far left side of the slide bar. PowerPoint manually gives each slide a sequential number (1, 2, 3, 4, 5, etc.).


Hope this is what you're looking for. If not, let me know, and I can try something else.

Chris

You Rock!!!! Chris.....!!!

Thank you so much from the bottom of my heart...!!!

you made our day with this Big Support and I appreciate your hard work on this...

I like both the way you have provided us Macro and we do have the option to remove the automatic Note section via Inspect presentation under File >>Info. option.

Thank you so much again!!! :):)
 
Upvote 0
You Rock!!!! Chris.....!!!

Thank you so much from the bottom of my heart...!!!

you made our day with this Big Support and I appreciate your hard work on this...

I like both the way you have provided us Macro and we do have the option to remove the automatic Note section via Inspect presentation under File >>Info. option.

Thank you so much again!!! :):)

Hi Chris,

I was using the above macro and was able to get the alternative slide but i am not able to get the same layout from the original slide.

i am using this macro provided by you

VBA Code:
Sub MergeAndAlternateSlides()
    Dim sourcePresA As Presentation
    Dim sourcePresB As Presentation
    Dim targetPres As Presentation
    Dim sourcePathA As String
    Dim sourcePathB As String
    Dim sourceSlide As Slide
    Dim targetSlide As Slide
    Dim i As Long
    Dim targetIndex As Long

    ' Set the paths to your A.pptx and B.pptx files
    sourcePathA = "C:\Users\gosains\Desktop\T\Macro\FileA.pptx"
    sourcePathB = "C:\Users\gosains\Desktop\T\Macro\FileB.pptx"

    ' Open the source presentations
    Set sourcePresA = Presentations.Open(sourcePathA)
    Set sourcePresB = Presentations.Open(sourcePathB)

    ' Create a new presentation to hold the merged slides
    Set targetPres = Presentations.Add

    ' Merge and alternate slides
    For i = 1 To sourcePresA.Slides.Count
        ' Copy slide from A.pptx
        sourcePresA.Slides(i).Copy
        targetIndex = (i - 1) * 2 + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.Shapes.Title.TextFrame.TextRange.Text = "A" & i

        ' Copy slide from B.pptx
        sourcePresB.Slides(i).Copy
        targetIndex = targetIndex + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.Shapes.Title.TextFrame.TextRange.Text = "B" & i
    Next i

    ' Close the source presentations without saving
    sourcePresA.Close
    sourcePresB.Close
End Sub
 
Upvote 0
Hi Chris,

I was using the above macro and was able to get the alternative slide but i am not able to get the same layout from the original slide.

i am using this macro provided by you

VBA Code:
Sub MergeAndAlternateSlides()
    Dim sourcePresA As Presentation
    Dim sourcePresB As Presentation
    Dim targetPres As Presentation
    Dim sourcePathA As String
    Dim sourcePathB As String
    Dim sourceSlide As Slide
    Dim targetSlide As Slide
    Dim i As Long
    Dim targetIndex As Long

    ' Set the paths to your A.pptx and B.pptx files
    sourcePathA = "C:\Users\gosains\Desktop\T\Macro\FileA.pptx"
    sourcePathB = "C:\Users\gosains\Desktop\T\Macro\FileB.pptx"

    ' Open the source presentations
    Set sourcePresA = Presentations.Open(sourcePathA)
    Set sourcePresB = Presentations.Open(sourcePathB)

    ' Create a new presentation to hold the merged slides
    Set targetPres = Presentations.Add

    ' Merge and alternate slides
    For i = 1 To sourcePresA.Slides.Count
        ' Copy slide from A.pptx
        sourcePresA.Slides(i).Copy
        targetIndex = (i - 1) * 2 + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.Shapes.Title.TextFrame.TextRange.Text = "A" & i

        ' Copy slide from B.pptx
        sourcePresB.Slides(i).Copy
        targetIndex = targetIndex + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.Shapes.Title.TextFrame.TextRange.Text = "B" & i
    Next i

    ' Close the source presentations without saving
    sourcePresA.Close
    sourcePresB.Close
End Sub
Hi Team

With the below code, i am able to get the same type of slide as original. and After generating the deck i am not able to save the file.

could you please help me on this

Here is the below code

VBA Code:
Sub MergeAndAlternateSlides()
    Dim sourcePresA As Presentation
    Dim sourcePresB As Presentation
    Dim targetPres As Presentation
    Dim sourcePathA As String
    Dim sourcePathB As String
    Dim sourceSlideA As Slide
    Dim sourceSlideB As Slide
    Dim targetSlide As Slide
    Dim i As Long
    Dim targetIndex As Long

    ' Set the paths to your A.pptx and B.pptx files
    sourcePathA = "C:\Users\gosains\Desktop\T\Macro\FileA.pptx"
    sourcePathB = "C:\Users\gosains\Desktop\T\Macro\FileB.pptx"

    ' Open the source presentations
    Set sourcePresA = Presentations.Open(sourcePathA, ReadOnly:=True, WithWindow:=False)
    Set sourcePresB = Presentations.Open(sourcePathB, ReadOnly:=True, WithWindow:=False)

    ' Create a new presentation to hold the merged slides
    Set targetPres = Presentations.Add

    ' Merge and alternate slides
    For i = 1 To sourcePresA.Slides.Count
        ' Copy slide from A.pptx
        Set sourceSlideA = sourcePresA.Slides(i)
        sourceSlideA.Copy
        targetIndex = targetPres.Slides.Count + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.Shapes.Title.TextFrame.TextRange.Text = "A" & i


        targetSlide.CustomLayout = sourceSlideA.CustomLayout

        ' Copy slide from B.pptx
        Set sourceSlideB = sourcePresB.Slides(i)
        sourceSlideB.Copy
        targetIndex = targetPres.Slides.Count + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.Shapes.Title.TextFrame.TextRange.Text = "B" & i

        ' Copy layout from B.pptx to the corresponding slide in the target presentation
        targetSlide.CustomLayout = sourceSlideB.CustomLayout
    Next i

    ' Close the source presentations without saving
    
End Sub
 
Upvote 0
Hi Team

With the below code, i am able to get the same type of slide as original. and After generating the deck i am not able to save the file.

could you please help me on this

Here is the below code

VBA Code:
Sub MergeAndAlternateSlides()
    Dim sourcePresA As Presentation
    Dim sourcePresB As Presentation
    Dim targetPres As Presentation
    Dim sourcePathA As String
    Dim sourcePathB As String
    Dim sourceSlideA As Slide
    Dim sourceSlideB As Slide
    Dim targetSlide As Slide
    Dim i As Long
    Dim targetIndex As Long

    ' Set the paths to your A.pptx and B.pptx files
    sourcePathA = "C:\Users\gosains\Desktop\T\Macro\FileA.pptx"
    sourcePathB = "C:\Users\gosains\Desktop\T\Macro\FileB.pptx"

    ' Open the source presentations
    Set sourcePresA = Presentations.Open(sourcePathA, ReadOnly:=True, WithWindow:=False)
    Set sourcePresB = Presentations.Open(sourcePathB, ReadOnly:=True, WithWindow:=False)

    ' Create a new presentation to hold the merged slides
    Set targetPres = Presentations.Add

    ' Merge and alternate slides
    For i = 1 To sourcePresA.Slides.Count
        ' Copy slide from A.pptx
        Set sourceSlideA = sourcePresA.Slides(i)
        sourceSlideA.Copy
        targetIndex = targetPres.Slides.Count + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.Shapes.Title.TextFrame.TextRange.Text = "A" & i


        targetSlide.CustomLayout = sourceSlideA.CustomLayout

        ' Copy slide from B.pptx
        Set sourceSlideB = sourcePresB.Slides(i)
        sourceSlideB.Copy
        targetIndex = targetPres.Slides.Count + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.Shapes.Title.TextFrame.TextRange.Text = "B" & i

        ' Copy layout from B.pptx to the corresponding slide in the target presentation
        targetSlide.CustomLayout = sourceSlideB.CustomLayout
    Next i

    ' Close the source presentations without saving
   
End Sub
Hi Team,

Can anyone help me with this?

Regards
Sanjeev
 
Upvote 0
Hi Team

With the below code, i am able to get the same type of slide as original. and After generating the deck i am not able to save the file.

could you please help me on this

Here is the below code

VBA Code:
Sub MergeAndAlternateSlides()
    Dim sourcePresA As Presentation
    Dim sourcePresB As Presentation
    Dim targetPres As Presentation
    Dim sourcePathA As String
    Dim sourcePathB As String
    Dim sourceSlideA As Slide
    Dim sourceSlideB As Slide
    Dim targetSlide As Slide
    Dim i As Long
    Dim targetIndex As Long

    ' Set the paths to your A.pptx and B.pptx files
    sourcePathA = "C:\Users\gosains\Desktop\T\Macro\FileA.pptx"
    sourcePathB = "C:\Users\gosains\Desktop\T\Macro\FileB.pptx"

    ' Open the source presentations
    Set sourcePresA = Presentations.Open(sourcePathA, ReadOnly:=True, WithWindow:=False)
    Set sourcePresB = Presentations.Open(sourcePathB, ReadOnly:=True, WithWindow:=False)

    ' Create a new presentation to hold the merged slides
    Set targetPres = Presentations.Add

    ' Merge and alternate slides
    For i = 1 To sourcePresA.Slides.Count
        ' Copy slide from A.pptx
        Set sourceSlideA = sourcePresA.Slides(i)
        sourceSlideA.Copy
        targetIndex = targetPres.Slides.Count + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.Shapes.Title.TextFrame.TextRange.Text = "A" & i


        targetSlide.CustomLayout = sourceSlideA.CustomLayout

        ' Copy slide from B.pptx
        Set sourceSlideB = sourcePresB.Slides(i)
        sourceSlideB.Copy
        targetIndex = targetPres.Slides.Count + 1
        targetPres.Slides.Paste targetIndex
        Set targetSlide = targetPres.Slides(targetIndex)
        targetSlide.Shapes.Title.TextFrame.TextRange.Text = "B" & i

        ' Copy layout from B.pptx to the corresponding slide in the target presentation
        targetSlide.CustomLayout = sourceSlideB.CustomLayout
    Next i

    ' Close the source presentations without saving
   
End Sub
Hi Team,

Can anyone help me with this...

Regards,
Sanjeev
 
Upvote 0

Forum statistics

Threads
1,214,986
Messages
6,122,611
Members
449,090
Latest member
vivek chauhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top