VBA PowerPoint unable to save presentation - vba

I'm trying to pull together a marco that add data and charts from Excel into a powerpoint template and save it in a directory. The macro is being run from Excel.
After running it, the following message appears:
Run-time error '-2147467259 (80004005)':
Presentation (unknown member): PowerPoint was unable to open or save this document. Please ensure that you have access priviliges to read or write the document that is not encrypted.
Below is the VBA code:
Sub CreateMPP()
Dim DestinationTemplate As String
Dim PowerPointApp As PowerPoint.Application
Dim myTemplate As PowerPoint.Presentation
DestinationTemplate = "C:\Users\Me\Documents\MPP_Template.potx"
Set PowerPointApp = CreateObject("PowerPoint.Application")
PowerPointApp.Visible = True
Set myTemplate = PowerPointApp.Presentations.Open(DestinationTemplate, ReadOnly:=msoFalse)
'Other macro that pulls the data and charts, which works perfectly fine
Call CreatePowerPointTest
myTemplate.SaveAs "TestTest", ppSaveAsPresentation
myTemplate.Close
PowerPointApp.Quit
End Sub

Related

Restart PowerPoint Presentation by a Macro

The Powerpoint-Presentation is running on two devices at the same time. So one of the presenations is in read-only mode.
I would like to have a macro, which updates the presentation in read-only-mode, so the changes on the presentation (write-mode) are applied.
The macro will be started manually by a button.
I already tried to write a macro that restarts the presentation, but wasn't successfull.
Sub Prog()
Dim DestinationPPT As String
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Set PowerPointApp = CreateObject("PowerPoint.Application")
DestinationPPT = "xxx.ppsm"
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)
myPresentation.SlideShowSettings.Run
Application.Quit
End Sub
Seems like the command Application.Quit is closing both presentations that are opened.
You need to close a certain file and not the whole application.
Dim PPTFile As Object
Set PPTFile = CreateObject("PowerPoint.Application").Presentations.Open(DestinationPPT)
PPTFile.Close
Use File.Close instead of Application.Quit
I hope this helped.

How to Open Embedded Object in Excel with Caption in Vba?

Try to open embedded object in excel file with caption,able to open file with name: can any one help how to open with caption or dynamically
Worksheets(SheetName).Activate
Set o = Worksheets(SheetName).OLEObjects("object 3")
o.Verb xlVerbOpen
MsgBox "Attachmene open"
Note: object will add continuously in excel file, how to find object dynamically to open with caption?
The embedded object in Excel is a Shape. Add two embeded workbooks in your ActiveSheet and try this code:
Public Sub TestMe()
Dim obj As Object
For Each obj In ActiveSheet.Shapes
Debug.Print obj.Application.Caption
Next obj
End Sub
Then try to change the code, with a simple condition, opening the obj, if the caption is the expected one:
If obj.Application.Caption = "someCaption" Then OpenTheWorkbook(obj)
At the end write some check to make sure that it skips some possible errors.
I open an embedded WORD document through Excel using the following verb command.
Set o = .OLEObjects("Object 1")
o.Verb xlVerbOpen
"Object 1" is the default name of the embedded object so that would be changed as needed.

Powerpoint VBA to switch back to powerpoint from Excel

I hope someone can help....
I have a powerpoint presentation, which has linked tables and graphs from an excel file. the updating of the slides are set to manual.
i have created a VBA code in Powerpoint which opens up the excel file. I am trying to update the links in the powerpoint through VBA instead of manually choosing each linked element and updating the values. while the first part of my VBA code works in opening up the excel file, the links are not being updated, which i think is down to not being back in the powerpoint to update the links, so I am trying to include in my VBA code lines which will go back to the powerpoint presentation, after which i assume the the line to update links will work (happy to be corrected). below is the code i have built so far....my comments are in bold ...
any suggestions?
FYI, I am using office 2007.
Thanks
Sub test()
Dim xlApp As Object
Dim xlWorkBook As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWorkBook = xlApp.Workbooks.Open("File location\filename.xlsm", True, False)
Set xlApp = Nothing
Set xlWorkBook = Nothing
Section above opens the excel file which contains the linked tables and charts
On Error Resume Next
With GetObject(, "PowerPoint.Application")
.ActivePresentation.SlideShowWindow.Activate
End With
Section above i was hoping would go back to the powerpoint after opening the excel file but it does not which is why i think the code below to update links is not working
ActivePresentation.UpdateLinks
End Sub
Start from something easier. This will allow you to activate the first existing PowerPoint application from Excel:
Option Explicit
Public Sub TestMe()
Dim ppt As New PowerPoint.Application
ppt.visible = msoTrue
ppt.Windows(1).Activate
End Sub
Then play a bit with it and fix it into your code.
#Vityata
Ok, i got it to work....original coding did the first part of opening the excel file, and to switch back to powerpoint (and i think this will only work if there is only 1 presentation open i added the following code...
AppActivate "Microsoft PowerPoint"
so my complete code looks like:
Sub test()
Dim xlApp As Object
Dim xlWorkBook As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWorkBook = xlApp.Workbooks.Open("file path\file name.xlsm", True, False)
Set xlApp = Nothing
Set xlWorkBook = Nothing
AppActivate "Microsoft PowerPoint"
End Sub
now to get manual links to update as part of the vba code...
If you capture the file that your macro is in. This is just a string of your path and filename
'This is the macro file
MacroFile = ActivePresentation.FullName
Then you can use that variable to activate just that specific PowerPoint presentation.
Use Presentations(MacroFile).Activate
or Presentations(MacroFile).Updatelinks
It's best not to use ActivePresentation when moving between applications.

Excel VBA Can't SaveAs Embedded PowerPoint Presentation in Office 2016

We embed pptx files in Excel, then use Excel VBA code (like below) to open, then SaveAs the pptx file to the user's drive, then programmatically modify the pptx content based on Excel calculations.
The Excel VBA code below works fine to control PowerPoint 2010 and 2013, but no longer works for PowerPoint 2016.
Note: I have similar code for Word and it works fine for Word 2016 (and prior versions).
Sub OpenCopyOfEmbeddedPPTFile() 'works with Office 2010 and 2013, but not on Office 2016
Dim oOleObj As OLEObject
Dim PPTApp As Object
Dim PPTPres As Object
Dim sFileName As String
Set oOleObj = ActiveSheet.Shapes("PPTObj").OLEFormat.Object 'name of the embedded pptx object
Set PPTApp = CreateObject("Powerpoint.Application")
PPTApp.Visible = True
sFileName = "C:\Users\Me\Documents\testPPT.pptx"
OleObj.Verb Verb:=xlVerbOpen 'it opens successfully
Set PPTPres = oOleObj.Object
PPTPres.SaveAs Filename:=sFileName 'fails here (in Office 2016)
PPTPres.Close
GetObject(, "PowerPoint.Application").Presentations.Open sFileName
'code to modify the Presentation (copy of the embedded pptx) based on Excel calculations
End Sub
Error:
Run-time error '-2147467259 (80004005)':
Presentation.SaveAs : An error occurred while PowerPoint was saving the file.
Also, the following PowerPoint VBA (not Excel VBA) works fine for normal documents (not embedded in Excel), but fails when I run it in an opened embedded pptm. Works fine in 2013 and 2010 embedded pptm's.
ActivePresentation.SaveAs FileName:="C:\Users\Me\Documents\testPPT.pptm"
Error:
Run-time error '-2147467259 (80004005)': Presentation (unknown member) : An error occurred while PowerPoint was saving the file.
Windows OS version does not seem to matter. Does not work on Office for Mac.
Any way to resolve or workaround this error? Or is there another way to do the same thing (modify a copy of the embedded pptx so the embedded pptx is not modified)? Or does this error only occur on our Office 2016 PCs?
PowerPoint 2016 appears to fail when using SaveAs or SaveCopyAs against an embedded presentation.
The workaround is to open the presentation, create a new presentation, and then copy the content from the embedded presentation to the new presentation. You can then close the embedded presentation, and save the new presentation as you wish.
I've demonstrated copying the slides, but you may need to programmatically copy BuiltInDocumentProperties and other non-slide content.
Option Explicit
Sub OpenCopyOfEmbeddedPPTFile() 'works with Office 2010 and 2013, but not on Office 2016
Dim oOleObj As OLEObject
Dim PPTApp As Object
Dim PPTPres As Object
Dim PPTNewPres As Object
Dim sFileName As String
Set oOleObj = ActiveSheet.Shapes("PPTObj").OLEFormat.Object 'name of the embedded pptx object
oOleObj.Verb 3
Set PPTPres = oOleObj.Object
Set PPTApp = PPTPres.Application
PPTApp.Visible = True
'We can't save the embedded presentation in 2016, so let's copy the clides to a new presentation
Set PPTNewPres = PPTApp.Presentations.Add
PPTPres.Slides.Range.Copy
PPTNewPres.Slides.Paste
'We may need to copy other things, like BuiltInDocumentProperties
'TODO
'Close the original
PPTPres.Close
sFileName = "C:\Users\Me\Documents\testPPT121.pptx"
sFileName = "C:\users\andrew\desktop\testPPT12111-FOOJANE.pptx"
PPTNewPres.SaveAs sFileName
'code to modify the Presentation (copy of the embedded pptx) based on Excel calculations
'Quit PPT
'PPTNewPres.Close
'PPTApp.Quit
End Sub
I found this possible answer in another forum and it worked for me saving a PPTM file as a PPTX file as a copy
Change
PPTPres.SaveAs Filename:=sFileName
to
PPTPres.SaveAs sFileName
Mine was :
PPTPres.SaveCopyAs sFileName
I then open the new file and close the PPTM file

Apply template to powerpoint in Excel VBA from embedded template file

I'm trying to apply a template to a powerpoint through excel. The powerpoint template is embedded within my excel file via insert -> Object. I've successfully used the .applytemplate method to apply a template from file, but I cannot adjust the code to reference the embedded powerpoint template. I tried using OLEObject, but I'm afraid that isn't correct. Please review below code.
Sub ppCreate()
Dim myPP As PowerPoint.Application
Dim myPres As PowerPoint.Presentation
Dim activeSlide As PowerPoint.Slide
Dim ppObj As OLEObject
' Create instance of PowerPoint
Set myPP = CreateObject("Powerpoint.Application")
' For automation to work, PowerPoint must be visible
myPP.Visible = True
' Create a presentation
Set myPres = myPP.Presentations.Add
' Set slide view to Slide Only
myPP.ActiveWindow.ViewType = ppViewSlide
'Resize to 4:3
myPres.PageSetup.SlideSize = 2
'Add a slide
Set activeSlide = myPres.Slides.Add(1, ppLayoutBlank)
'Import Template
Worksheets("CBRDATA").Select
Set ppObj = ActiveSheet.OLEObjects("ppObj") 'NOT WORKING
myPres.ApplyTemplate (ppObj) 'NOT WORKING
myPres.ApplyTemplate "C:\CBR_TEMPLATE_COVER.potx" 'WORKING
Worksheets("CBR").Select
End Sub
Update:
'Test if template exists in C:\
If Dir("C:\CBR_TEMPLATE_COVER.potx") = "" Then
'Open/Save the embedded template to C:\
Set tempPP = CreateObject("Powerpoint.Application")
Worksheets("CBRDATA").OLEObjects("ppObj").Verb 0
tempPP.Visible = True
Set tempPres = tempPP.ActivePresentation
tempPres.SaveCopyAs ("C:\CBR_TEMPLATE_COVER.potx")
tempPres.Close
Else:
End If
' Create instance of PowerPoint
Set myPP = CreateObject("Powerpoint.Application")
This doesn't work because ActiveSheet.OLEObjects("ppObj") is type OLEObject, not PowerPoint.Presentation.
Set ppObj = ActiveSheet.OLEObjects("ppObj") 'NOT WORKING
While manually double-clicking on the object open the POTX file (actually it opens a new blank PPTX using the POTX as template), your assignment statement above isn't doing any of that, it's trying instead to put an OLEObject where a Presentation is expected, and that will always fail.
So, how to "open" the OLEObject? OLEObject has a .Verb method, and the following will perform the object's default action, which in the case of embedded package objects, is usally to "open" them.
The Solution
'Import Template
'## This should Open the template
Worksheets("CBRDATA").OLEObjects("ppObj").Verb 0
'## Assign the ActivePresentation to your ppObj variable
Set ppObj = myPP.ActivePresentation
Editorializing: Embedded OLEObjects are notoriously problematic, and probably not an ideal place to story things like document templates :)