VBA 尝试设置 PowerPoint 幻灯片时出错 "Expected Function or Variable"
VBA Error "Expected Function or Variable" when trying to set a PowerPoint Slide
我有以下代码 -
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
我收到一个错误
Expected Function or Variable
在以下行:
Set oPPTSlide = oPPTFile.Slides(SlideNum).Select
如有任何帮助,我们将不胜感激。
应该是...
Set oPPTSlide = oPPTFile.Slides(SlideNum)
按照我上面的评论,你不能在同一行 Set
和 Select
(而且,几乎没有任何理由使用 Select
)。尝试 Set oPPTSlide = oPPTFile.Slides(SlideNum)
然而,一些"upgrades"你的代码:
直接将 oPPTShape
设置为新创建的 Shapes
with :
Set oPPTShape = oPPTSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 10, 20, 300, 5)
然后,使用下面的 With
语句轻松修改 oPPTShape
属性:
With oPPTShape.TextFrame.TextRange
.text = "ALL BSE"
.Font.Color = vbWhite
.Font.Underline = msoFalse
End With
我有以下代码 -
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
我收到一个错误
Expected Function or Variable
在以下行:
Set oPPTSlide = oPPTFile.Slides(SlideNum).Select
如有任何帮助,我们将不胜感激。
应该是...
Set oPPTSlide = oPPTFile.Slides(SlideNum)
按照我上面的评论,你不能在同一行 Set
和 Select
(而且,几乎没有任何理由使用 Select
)。尝试 Set oPPTSlide = oPPTFile.Slides(SlideNum)
然而,一些"upgrades"你的代码:
直接将 oPPTShape
设置为新创建的 Shapes
with :
Set oPPTShape = oPPTSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 10, 20, 300, 5)
然后,使用下面的 With
语句轻松修改 oPPTShape
属性:
With oPPTShape.TextFrame.TextRange
.text = "ALL BSE"
.Font.Color = vbWhite
.Font.Underline = msoFalse
End With