4

我必须使用 excel-vba 将文本从 Excel 中的单元格复制到 PPT 中的文本框。我有以下代码:

 ActivePresentation.Shapes(tb).TextFrame.Characters.Text = ActiveSheet.Range("C41").Value

但是此代码为 Shapes 提供了错误“找到方法或数据成员机器人”。正确的方法是什么?

提前致谢

4

1 回答 1

3

您收到错误消息,因为您没有指定形状的位置。我是说哪张幻灯片???

请参阅此示例。(从 Excel 中尝试和测试)

酌情修改。

代码:

Option Explicit

Sub Sammple()
    Dim oPPApp As Object, oPPPrsn As Object, oPPSlide As Object
    Dim oPPShape As Object
    Dim FlName As String

    '~~> Change this to the relevant file
    FlName = "C:\MyFile.PPTX"

    '~~> Establish an PowerPoint application object
    On Error Resume Next
    Set oPPApp = GetObject(, "PowerPoint.Application")

    If Err.Number <> 0 Then
        Set oPPApp = CreateObject("PowerPoint.Application")
    End If
    Err.Clear
    On Error GoTo 0

    oPPApp.Visible = True

    '~~> Open the relevant powerpoint file
    Set oPPPrsn = oPPApp.Presentations.Open(FlName)
    '~~> Change this to the relevant slide which has the shape
    Set oPPSlide = oPPPrsn.Slides(1)
    '~~> Change this to the relevant shape
    Set oPPShape = oPPSlide.Shapes(1)

    '~~> Write to the shape
    oPPShape.TextFrame.TextRange.Text = _
    ThisWorkbook.Sheets("Sheet1").Range("C41").Value

    '
    '~~> Rest of the code
    '
End Sub
于 2013-11-05T09:04:39.247 回答