The aim is, that I have both (Excel and PowerPoint) open simultaneously. So when I run my Excel macro the PowerPoint should be updated and saved in the same file - no reopening and then some kind of SaveAs
...
I know / suppose that the solution of my question lies in Set oPresentation = appPPT.Presentations.Open(sPPTfile, msoFalse)
- the Presentations.Open
-method. But I have no clue how to overcome this issue. I found nothing appropriate.
So here my code and many thanks for your hints and comments in advance.
Sub openPPT()
Dim appPPT As PowerPoint.Application
Dim slide As PowerPoint.Slide
Dim oPresentation As PowerPoint.Presentation
Dim txtFeld(12) As PowerPoint.Shape
Dim sPPTfile As String
Dim wkb As Workbook
Dim wks As Worksheet
On Error GoTo err
Set wkb = ThisWorkbook
Set wks = wkb.Worksheets(1)
Set appPPT = New PowerPoint.Application
sPPTfile = "C:\Users\xxx\TestPowerPoint.pptx"
appPPT.Visible = True
' here is the Presentations.Open method which causes probably my issue
Set oPresentation = appPPT.Presentations.Open(sPPTfile, msoFalse)
Set slide = appPPT.ActivePresentation.Slides(3)
slide.Shapes("Rectangle 32").TextFrame.TextRange.Text = Tabelle1.Range("E10")
' in the following two lines of code an error message arises in Excel
oPresentation.Save
' appPPT.ActivePresentation.Save ' ed2 suggested this line of code
err:
If err.Number <> 0 Then
MsgBox err.Number & vbCrLf & err.Description
End If
Set slide = Nothing: Set appPPT = Nothing: Set oPresentation = Nothing
End Sub
Edit_1: The error message
The following error message arises when using oPresentation.Save
or appPPT.ActivePresentation.Save
(which ed2 suggested).
Here on this forum a user is also questioning of this "Err.Number = -2147467259" but this error message doesn't exist in the recommended list... Perhaps that's also a hint?!
Edit_2: When using the proposed code of FunThomas also an error message arises. This time not in Excel, but in Excel VBA (see screenshot) and it is caused by oPresentation.Save
.
Edit_3 and solution: The issue why also FunThomas code is failing is caused by the file naming.
This code of line If oPresentation.FullName = sPPTfile Then
was never True
because
oPresentation.FullName
is https://xxx/TestPowerPoint.pptx andsPPTfile
is C:\Users\xxx\TestPowerPoint.pptx
So when you
- copy your file path out of the explorer you get C:\Users\xxx\TestPowerPoint.pptx
- right click on the file (in the explorer) and choose copy link you get some insane https://xxx/... stuff
- run the code and extract the correct name out of
oPresentation.FullName
. This needs some extra preparation... but it's worth ;-)