Sumit Kumawat
Sumit Kumawat

Reputation: 15

VBA Error "Expected Function or Variable" when trying to set a PowerPoint Slide

I have following code -

Option Explicit

Sub main()

Dim oPPTApp As PowerPoint.Application
Dim oPPTObj As Object
Dim oPPTFile As PowerPoint.Presentation
Dim oPPTShape As PowerPoint.Shape
Dim oPPTSlide As PowerPoint.Slide
Dim oGraph As Graph.Chart
Dim oAxis As Graph.Axis
Dim SlideNum As Integer
Dim strPresPath As String, strNewPresPath As String

strPresPath = "Location.ppt"
strNewPresPath = "Destination.ppt"

'instantiate the powerpoint application and make it visible
Set oPPTObj = CreateObject("PowerPoint.Application")
oPPTObj.Visible = msoCTrue

Set oPPTFile = oPPTObj.Presentations.Open(strPresPath)
SlideNum = 1

Set oPPTSlide = oPPTFile.Slides(SlideNum).Select
Set oPPTShape = oPPTSlide.Add(1, ppLayoutBlank)

oPPTSlide.Shapes.AddTextbox msoTextOrientationHorizontal, 10, 20, 300, 5

With oPPTSlide.Shapes(1).TextFrame.TextRange
    .text = "ALL BSE"
    .Font.Color = vbWhite
    .Font.Underline = msoFalse
End With

End Sub

I get an error

Expected Function or Variable

at the following line:

Set oPPTSlide = oPPTFile.Slides(SlideNum).Select 

Any help would be appreciated.

Upvotes: 1

Views: 312

Answers (2)

Shai Rado
Shai Rado

Reputation: 33692

Following my comment above, you can't Set and Select at the same line (also, there's almost never any reason to use Select). Try Set oPPTSlide = oPPTFile.Slides(SlideNum)

However, a few "upgrades" to your code:

Directly set the oPPTShape with the new created Shapes with :

Set oPPTShape = oPPTSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 10, 20, 300, 5)

and afterwards, easily modify the oPPTShape properties, using the With statement below:

With oPPTShape.TextFrame.TextRange
    .text = "ALL BSE"
    .Font.Color = vbWhite
    .Font.Underline = msoFalse
End With

Upvotes: 2

Subodh Tiwari sktneer
Subodh Tiwari sktneer

Reputation: 9976

Should be...

Set oPPTSlide = oPPTFile.Slides(SlideNum)

Upvotes: 2

Related Questions