Log in

View Full Version : VBA - Powerpoint Addtext in existing file



kato01
03-10-2014, 12:00 PM
Hello,

I am just trying to ad a new slide plus some text to an exiting ppt file.
I can't understand why this does not work.

Please advise

Thank you
Kato01


Sub writeToPPT()
'Add a reference to Microsoft Powerpoint 12.0 object library

Dim oPPTApp As PowerPoint.Application
Dim oPPTFile As PowerPoint.Presentation
Dim oPPTShape As PowerPoint.Shape
Dim oPPTSlide As PowerPoint.Slide
Set oPPTApp = CreateObject("PowerPoint.Application")
oPPTApp.Visible = msoTrue
'opening an existing presentation
'Change the path and presentation name
Set oPPTFile = oPPTApp.Presentations.Open(Filename:="Y:\D\(0)_OFFICE\MySimulations\My Simulations 20140204.pptM")
oPPTFile.Slides.Add Index:=oPPTFile.Slides.Count + 1, Layout:=ppLayoutBlank
NS = oPPTFile.Slides.Count
'Write to the first shape on first slide
Set oPPTShape = oPPTFile.Slides(2).Shapes(1)
With oPPTShape.TextFrame.TextRange
.Font.Size = 12
.Font.Color = vbRed
.Font.Underline = msoTrue
.Text = "Add some sample text." & vbCrLf _
& "Line 2 to add some more text" & vbCrLf _
& "Line 3 to add even more text"

End With
'Add cleanup code
End Sub

John Wilson
03-11-2014, 01:32 AM
Is this code running within PowerPoint or for example from Excel etc?
Unless this is running outside PowerPoint there is no need for a reference or to create an application.

The main problem though is that you are creating a blank slide (with no shapes) and trying to add to a non existant shape 1. (*assuming slides 2 is the added slide which it might not be as your code doesn't make a lot of sense.)