ante011
ante011

Reputation: 99

Overwrite when paste to powerpoint VBA

If i have texts(shapes) in my powerpoint. How can i modify my code NOT to create a new shape but overwrite the value inside it.

The code below creates a new shape. Any Ideas?

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

Upvotes: 1

Views: 174

Answers (2)

John Coleman
John Coleman

Reputation: 51998

You need to find the index of the shape you want to paste into. Say it is index 2

Then use

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

Upvotes: 1

ebrts
ebrts

Reputation: 381

lets use a variable instead of copying and pasting like this

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

you can then set the text value of a shape like this

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

read about how to set the name of a shape here: How to name an object within a PowerPoint slide?

Upvotes: 1

Related Questions