VBA错误"预期功能或变量"在尝试设置PowerPoint幻灯片时

时间:2017-04-11 12:01:17

标签: excel vba excel-vba powerpoint-vba

我有以下代码 -

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

我收到错误

  

预期功能或变量

在以下行:

Set oPPTSlide = oPPTFile.Slides(SlideNum).Select 

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

应该......

Set oPPTSlide = oPPTFile.Slides(SlideNum)

答案 1 :(得分:2)

根据我上面的评论,你不能SetSelect在同一行(同样,几乎没有任何理由使用Select)。试试Set oPPTSlide = oPPTFile.Slides(SlideNum)

但是,对您的代码进行一些“升级”:

使用新创建的oPPTShape直接设置Shapes

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

然后,使用下面的oPPTShape声明轻松修改With属性:

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