首页 > 解决方案 > 尽管操作成功,但对象需要错误

问题描述

我想构建由一组 12 个元素组成的许多不同的 PowerPoint 幻灯片(我称它们为“模板”)——对于每张幻灯片,只有元素的顺序和元素中的文本会发生变化。

目标:
遍历 Excel 中的行。每一行代表“模板”的一种用法。在第 5 列中,它说明了要使用的“模板”(来自 ID),然后其他列包含字段的文本。
所有模板都在演示文稿的最后一张幻灯片上,我从那里复制相关模板,粘贴并用文本填充。

其中一个模板就是一个文本框(标题)。所有其他都是形状组。

当我尝试运行此代码时,它告诉我该行的 424“需要对象”

 .TextFrame.TextRange.Text = wks.Range(line, CLMN).Text

...但是在它成功运行之后。标题在幻灯片上,文本是正确的。

事后怎么可能需要一个对象?

完整的子:

Sub AddShape(typ As Integer, state As Integer, currentSld As Slide, height As Long, line As Integer)

    'Set the constants (although not implemented as Const)
    Dim CLMN As Integer
    CLMN = 5
    Dim stencils As Shapes
    Dim stencilSlide As Integer
    stencilSlide = CInt(ActivePresentation.Slides.Count)
    Set stencils = ActivePresentation.Slides(stencilSlide).Shapes

    Dim HEADER As Shape
    Set HEADER = stencils("header")

    Dim ALPHANUMERICAL As Shape
    Set ALPHANUMERICAL = stencils("alphanumerical")

    Dim BIRTHDATE As Shape
    Set BIRTHDATE = stencils("birthdate")

    Dim TOGGLE As Shape
    Set TOGGLE = stencils("toggle")

    Dim DROPDOWN As Shape
    Set DROPDOWN = stencils("dropdown")

    Dim NUMERICAL As Shape
    Set NUMERICAL = stencils("numerical")

    Select Case typ
    Case 1
        ALPHANUMERICAL.Copy
    Case 2
        NUMERICAL.Copy
    Case 4
        DROPDOWN.Copy
    Case 5
        TOGGLE.Copy
    Case 9
        BIRTHDATE.Copy
    End Select

    If typ = 10 Then
        HEADER.Copy
        With currentSld.Shapes.Paste
            .Top = height
            .TextFrame.TextRange.Text = wks.Range(line, CLMN).Text
        End With
    Else
        With currentSld.Shapes.Paste
            .Top = height
            For x = 1 To .GroupItems.Count
                If .GroupItems(x).Name = "label" Then
                    With .GroupItems(x)
                        .TextFrame.TextRange.Text = wks.Range(line, CLMN).Text
                    End With
                Else
                    With .GroupItems(x)
                        .TextFrame.TextRange.Text = wks.Range(line, CLMN + CInt(.GroupItems(x).Name)).Text
                    End With
                End If
            Next
        End With
    End If

End Sub

在此处输入图像描述

标签: excelvbapowerpoint

解决方案


尝试使用wks.Cells(line, CLMN).Value代替wks.Range(line, CLMN).Text

我认为lineandCLMN是 Long 变量,代表Rowand Column

但是,如果lineaString代表地址的文字部分(如“A”、“B”、“AB”),则必须使用wks.Range(line & CLMN).Value


推荐阅读