【问题标题】:Setting the name of an object in Powerpoint as a String将 Powerpoint 中的对象名称设置为字符串
【发布时间】:2020-07-23 00:41:53
【问题描述】:

我有一个包含大约 3000 张幻灯片的 powerpoint 幻灯片。在每张幻灯片上,我都有一张图片和一个文本框。在 powerpoint 中,图片在选择窗格中附加了一个特定的名称,而文本框名为“TextBox 3”。每个文本框都包含文本“2013-09-27 16.27.54”。我的工作是浏览每个文本框,并用选择窗格中的图片名称替换该文本。我已经编写了以下代码来执行此操作,但是我无法将图片的名称设置为字符串。当我运行此代码时,我在第 10 行得到“编译错误:无效的限定符”,并且名称在第 10 行突出显示

如何消除此错误?我假设这是因为对象的名称未被识别为字符串。

我的代码如下:

Sub Hello()


For Each sld In Application.ActivePresentation.Slides
    For Each shp In sld.Shapes
        If shp.HasTextFrame Then
            Set txtRng = shp.TextFrame.TextRange
            Set foundText = txtRng.Find(FindWhat:="2013-09-27 16.27.54")
            Do While Not (foundText Is Nothing)
                With foundText
                .Replace(FindWhat:=foundText, _
          Replacewhat:=Application.ActivePresentation.Slides(i).Shapes(1).Name.TextRange.TextFrame, WholeWords:=True) = True
            
             
                End With
            Loop
        End If
End If
Next
Next
End Sub

【问题讨论】:

  • 对象名称是一个字符串...您收到错误消息吗?如果是,是什么错误,在哪一行?如果不是,“我的代码不起作用”是什么意思?您的代码在循环中使用shp.TextFrame,其中shp.HasTextFrame = False...
  • 在您从代码中删除某些内容后,告诉我们第 12 行的情况不是很清楚...尝试在此处复制讨论中的代码行。但是你的代码漏掉了一个重要的问题:既然有文本框和图片,都是形状,那么你首先要确定要处理的形状是什么类型。那么Set NameofPicture = shp.Name 是错误的,因为“名称”属性不是对象。我建议您在模块顶部使用Option Explicit,声明所有涉及的变量,然后再尝试运行和调试。并在调试时排除所有幻灯片之间的迭代。

标签: vba powerpoint


【解决方案1】:

查找和替换是不必要的复杂。找到图片,得到它的名字,找到文本框,替换文本:

Sub Hello()
Dim sld As Slide
Dim shp As Shape
Dim PicName As String

    For Each sld In Application.ActivePresentation.Slides
        For Each shp In sld.Shapes
            If shp.Type = msoPicture Then
                PicName = shp.Name
            End If
        Next
        For Each shp In sld.Shapes
            If shp.HasTextFrame Then
                shp.TextFrame.TextRange.Text = PicName
            End If
        Next
    Next
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    相关资源
    最近更新 更多