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

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

Related

How to make a Macro-Free backup copy of a Word Document using VBA

I have a Macro-Enabled word document which I want to make a Macro-Free Backup Copy of my document else where on the network every time I press a certain commandbutton on my userform. I Don't want to use ".SaveAs2" because I still want to be in Macro-Enabled document after I hitting commandbutton.
In Excel we had this workaround which you could copy all sheets using "sheets.copy" on macro-enabled excel file and then save newly created workbook (which is now a macro-free workbook) anywhere you want using "ActiveWorkbook.SaveAs" method.
Try this.
Sub Word_CopyFileBasedOnActiveDocument()
Dim wDoc As Word.Document: Set wDoc = ActiveDocument
Dim wTmp As Word.Document
' Create a new document based on the active document
Set wTmp = Application.Documents.Add(Template:=wDoc.FullName, Visible:=False)
' Save as non macro enabled
wTmp.SaveAs2 Filename:=wDoc.Path & "\newFile.doc", FileFormat:=wdFormatXMLDocument
' Close file
wTmp.Close False
End Sub

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.

VBA PowerPoint unable to save presentation

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

word vba wont close excel application

How can I close excel aplication completely in word vba?
My code:
Dim ExcelApp As Object, ExcelBook As Object
Set ExcelApp = CreateObject("excel.Application")
Set ExcelBook = ExcelApp.Workbooks.Open(ActiveDocument.Path & "\data.xls")
'ExcelApp.Visible = True/False
ExcelApp.DisplayAlerts = False
'something with ExcelBook, just editing cells
ExcelBook.Close savechanges:=True
Excel.Application.Quit
Set ExcelBook = Nothing
Set ExcelApp = Nothing
When I open taskmanager there is still proces EXCEL running. Problem is, that I need to run macro multiple times and than my data.xls - excel file can be open read only.
You need to quit the instance you started. Replace
Excel.Application.Quit
with
ExcelApp.Quit
I find out why I had this problem. While running code that uses Automation to control Microsoft Excel, Visual Basic does not release reference until you end the program.
When I close word, excel process is closed as well.
More info here:
http://support.microsoft.com/kb/178510/en-us

PowerPoint 2010 Macro only works under Windows 7, not Windows XP, (Copy, Paste Shapes error/bug)

I migrated a VBA macro from PowerPoint 2007 to 2010 and after some debugging and testing I found out that copying shapes from one presentation to another only works under Windows 7 and not under Windows XP.
When I close the presentation I copied the shape from, in Windows XP, the new object is emptied. In Windows 7 the object still exists.
I made an example:
Public Sub test()
Dim HandlerApplication As New HandlerApplication
Dim slide As slide
HandlerApplication.create Application
Set slide = ActiveWindow.Selection.SlideRange(1)
Dim ppt As Presentation
Dim shapeToCopy As shape
Dim copiedShape As shape
Dim strTemplateFile As String
strTemplateFile = "SlideTemplate.pptx"
Set ppt = Application.Presentations.Open(BuildPath(ComponentsPath, strTemplateFile), msoTrue, msoFalse, msoFalse)
Set shapeToCopy = ppt.slides(7).shapes(1)
shapeToCopy.Copy
Set copiedShape = slide.shapes.PasteSpecial(ppPasteShape)(1)
ppt.Close
copiedShape.Name = "TestName"
End Sub
I appreciate any help/workaround.
The problem was a bug in Office 2010 and has been fixed in this hotfix: http://support.microsoft.com/kb/2345341
Try shapeToCopy.Duplicate instead.