我必须使用excel-vba将文本从Excel中的单元格复制到PPT中的文本框中。 我有以下代码:
ActivePresentation.Shapes(tb).TextFrame.Characters.Text = ActiveSheet.Range("C41").Value
但是这段代码给出了Shapes的错误“方法或数据成员bot找到”。这样做的正确方法是什么?
提前致谢
答案 0 :(得分: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