vba 将文本从 Excel 单元格复制到 PPT 文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19784799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
copy text from Excel cell to PPT textbox
提问by user2862496
I have to copy text from a cell in Excel to a textbox in PPT using excel-vba. I have the following code:
我必须使用 excel-vba 将文本从 Excel 中的单元格复制到 PPT 中的文本框。我有以下代码:
ActivePresentation.Shapes(tb).TextFrame.Characters.Text = ActiveSheet.Range("C41").Value
But this code is giving the error "method or data member bot found" for Shapes. What is the correct way to do it?
但是此代码为 Shapes 提供了错误“找到的方法或数据成员机器人”。正确的做法是什么?
Thanks in advance
提前致谢
采纳答案by Siddharth Rout
You are getting an error becuase you have not specified where the shape is. I mean which slide???
您收到错误,因为您没有指定形状的位置。我的意思是哪个幻灯片???
See this example. (tried and tested from Excel)
请参阅此示例。(从 Excel 中尝试和测试)
Amend as applicable.
适用时进行修改。
Code:
代码:
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