粘贴到 PowerPoint 时覆盖 VBA

Overwrite when paste to powerpoint VBA

如果我的 powerpoint 中有文本(形状)。我怎样才能修改我的代码而不是创建新形状而是覆盖其中的值。

下面的代码创建了一个新形状。 有什么想法吗?

Private Sub test()
Dim xlApp As Object
Dim xlWorkBook As Object

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlWorkBook = xlApp.Workbooks.Open("D:\ELE_powerpoint\book1.xlsx", True, False)

With xlWorkBook.ActiveSheet
xlWorkBook.sheets(1).Range("A2").Copy
End With

ActivePresentation.Slides(1).Shapes.Paste.Select

Set xlApp = Nothing
Set xlWorkBook = Nothing


End Sub

让我们使用变量而不是像这样复制和粘贴

mytext = xlWorkBook.sheets(1).Range("A2")

然后您可以像这样设置形状的文本值

ActivePresentation.Slides(1).Shapes("Shape name").TextFrame.Characters.Text = myText

在此处阅读有关如何设置形状名称的信息:How to name an object within a PowerPoint slide?

您需要找到要粘贴到的形状的索引。假设它是索引 2

然后使用

ActivePresentation.Slides(1).Shapes(2).TextFrame.TextRange.Paste