将文本从Excel单元格复制到PPT文本框
问题描述:
我必须使用excel-vba将文本从Excel中的单元格复制到PPT中的文本框。
我有以下代码:
I have to copy text from a cell in Excel to a textbox in PPT using excel-vba. I have the following code:
ActivePresentation.Shapes(tb).TextFrame.Characters.Text = ActiveSheet.Range("C41").Value
但是这个代码是给出错误方法或数据成员bot发现为形状。有什么正确的方法?
But this code is giving the error "method or data member bot found" for Shapes. What is the correct way to do it?
提前感谢
答
p>您收到错误,因为您没有指定形状在哪里。我的意思是哪个幻灯片???
You are getting an error becuase you have not specified where the shape is. I mean which slide???
看这个例子。 (从Excel中测试过)
See this example. (tried and tested from 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