Excel VBA that Opens a ppt file and splits file out

bbrimberry

New Member
Joined
Mar 23, 2016
Messages
34
Hello, Best VBAers around.

I am hoping someone out there can help me.

I have some excel vba code that opens a ppt file and splits it into parts based on a textbox value.
I need to keep the original formatting of the presentation the same.
sometimes it runs fine, other times I get an error that says " Something went wrong that might make PowerPoint Unstable"

any words of wisdom would be greatly appreciated



VBA Code:
Sub CopySlides()

 

Dim pptApp As PowerPoint.Application

Dim pptPres As PowerPoint.Presentation

Dim newPres As PowerPoint.Presentation

Dim slide As PowerPoint.slide

Dim subbanner As PowerPoint.shape

Dim lastSubbannerText As String

Dim filePath As String

 

 

'Open the PowerPoint presentation

Set pptApp = CreateObject("PowerPoint.Application")

 

 

filePath = Range("a12").Value

Set pptPres = pptApp.Presentations.Open(filePath)

 

 

 


 

'Create a new presentation

Set newPres = pptApp.Presentations.Add

 

'Loop through each slide in the presentation

For Each slide In pptPres.Slides

'Find the subbanner shape on the slide

Set subbanner = slide.Shapes.Range(Array("subbanner")).Item(1)

'If the subbanner text on the current slide is different from the last slide, save and close the current presentation and create a new one

If subbanner.TextFrame.TextRange.Text <> lastSubbannerText Then

'Save and close the current presentation

'newPres.SaveAs "C:\Path\To\Save\Presentations" & lastSubbannerText & ".pptx"

'newPres.Close

'Create a new presentation

Set newPres = pptApp.Presentations.Add

End If

'Copy the slide to the clipboard

slide.Copy

'Paste the slide into the new presentation with the source formatting

pptApp.CommandBars.ExecuteMso ("PasteSourceFormatting")

'newPres.Item(1).Slides.Item(newPres.Item(1).Slides.Count).Design = pptPres.Slides.Item(i).Design

'Save the text of the current subbanner for comparison with the next slide

lastSubbannerText = subbanner.TextFrame.TextRange.Text

Next

 

'Save and close the final presentation

'newPres.SaveAs "C:\Path\To\Save\Presentations" & lastSubbannerText & ".pptx"

'newPres.Close

 

End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try pausing your macro and calling DoEvents after you copy, and after you paste. So, for example, first add the following code to your module...

VBA Code:
Sub PauseMacro(ByVal secs As Long)

    Dim endTime As Single
    endTime = Timer + secs
    
    Do
        DoEvents
    Loop Until Timer > endTime
    
End Sub

Then in your code would have the following...

VBA Code:
'your code
'
'

slide.Copy

PauseMacro 3 'seconds

pptApp.CommandBars.ExecuteMso ("PasteSourceFormatting")

PauseMacro 3 'seconds

'
'
'

Note that the macro is paused for 3 seconds each time PauseMacro is called. However, you might find that a 1 second delay will suffice.

Hope this helps!
 
Upvote 0
Solution
At first glance, this seems to have solved the issue.
I'm going to keep testing, but I cannot thank you enough.
I had been working on this off and on for a week or so.
 
Upvote 0
That's great, and thanks for your feedback.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,215,692
Messages
6,126,230
Members
449,303
Latest member
grantrob

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