尝试设置 PowerPoint 幻灯片时出现 VBA 错误 "Expected Function or Variable"

标签 vba excel powerpoint

我有以下代码 -

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 

任何帮助,将不胜感激。

最佳答案

按照我上面的评论,你不能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

关于尝试设置 PowerPoint 幻灯片时出现 VBA 错误 "Expected Function or Variable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43345320/

相关文章:

vba - 在 Excel 中使用 VBA 通过索引 # 选择字典项

vba - 将变量值插入带有文本VBA的单元格

vba - 将所有命名范围提取到一个类中

vba - 使用 3 个小数点从 Excel 更新 PowerPoint

Python,如何在Powerpoint模板中插入值?

java - 使用 apache poi 从 pptx 中提取图像

excel - 运行时错误 1004 无法使用 url 链接名称查找列

vba - 从列表框写入后刷新工作表

vba - 获取承载当前运行的 VBA 代码的工作簿的名称?

excel - 对非连续命名范围进行平均