【问题标题】:ActiveX error in VBA code to replace words in a PowerpointVBA 代码中的 ActiveX 错误以替换 Powerpoint 中的单词
【发布时间】:2020-03-05 17:57:54
【问题描述】:

我有一个 VBA 代码,它应该在 powerpoint 中用另一个词替换一个词。然而,当我尝试运行它时,我收到错误 429“ActiveX 组件无法创建对象”,但我不知道为什么。这是代码:

Sub pres()

    Dim PowerPointApp As Object
    Set PowerPointApp = CreateObject("PowerPoint.Application")

    Dim myPres As Object
    Set myPres = PowerPointApp.Presentations.Open("C:\Users\NAME\Desktop\PRESVBA\Pres.pptx")

    Dim sld As Slide
    Set sld = ActivePresentation.Slides(3)
    Dim shp As Shape

    For Each shp In sld.Shapes
    If shp.HasTextFrame Then
        If shp.TextFrame.HasText Then
            shp.TextFrame.TextRange.Text = Replace(shp.TextFrame.TextRange.Text, "Montant", "Amount")
     End If
    End If
    Next shp
End Sub

【问题讨论】:

  • 这个错误出现在哪一行?你有PPT吗?
  • 在 Set sld = ActivePresentation.Slides(3) 行,我有 Powerpoint,它打开了正确的文件,但我得到了错误
  • 你打开的PowerPoint中有3张幻灯片吗?
  • 不,代码应该改变幻灯片编号 3 的文字
  • 你需要在前面加上ActivePresentation.Slides(3)你的powerpointapp

标签: excel vba


【解决方案1】:

我认为您正在尝试从 excel 更新幻灯片,如果是,请按照步骤操作

在 excel vba 中,转到工具 > 参考 > 检查 Microsoft PowerPoint 16.0 对象库(适用于 Office 2019),如果 Office 版本较低,您会发现类似情况。点击确定

下面的代码

Sub pres()

    Dim PowerPointApp As Object
    Set PowerPointApp = CreateObject("PowerPoint.Application")

    Dim myPres As Object
    Set myPres = PowerPointApp.Presentations.Open("C:\Personal\test\Presentation1.pptx")

    Dim sld As PowerPoint.Slide '<- change here
    Set sld = myPres.Slides(1) '<- number in brackets indicate the slide number
    Dim shp As PowerPoint.Shape '<- change here

    For Each shp In sld.Shapes
        If shp.HasTextFrame Then
            If shp.TextFrame.HasText Then
                shp.TextFrame.TextRange.Text = Replace(shp.TextFrame.TextRange.Text, "Montant", "Amount")
            End If
        End If
    Next shp
End Sub

如果它解决了痛苦,请标记为答案:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 2020-03-27
    • 2023-03-30
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多