Excel to Powerpoint (But with tables not pictures) - excel

I am attempting to automate powerpoint creation with table created in Excel. My issue is after the table pastes into powerpoint, it is not considered 'selected' right away. I get an error that generally that says something like 'no object selected' or "Method 'ShareRange' of object 'Selection' failed". If I slowly go through the code with F8 it works, for the most part, it's when it runs it doesn't work.
In any case I've tried using the table's name (which when copies in the table gets the name 'table 1' imagine that), I've tried having it wait a few seconds and a few other things. I've tried different types of pasting into the file, however I need to keep it a table, not a picture (I have my code working if I only needed a picture). My issue (I think) is that it pastes and is not selected immediately.
I have modified it a bit and just am showing where the errors are, I also do this same paste, move, size, over and over. I really hope it's a 1 line - smack my face because it's obvious fix...
Dim pp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim Slide1Title As Excel.Range
'Opening a blank, normally I have it open a template
Set pp = New PowerPoint.Application
Set PPPres = pp.Presentations.Add
Set PPSlide = PPPres.Slides.Add(1, 12)
Set PPSlide = PPPres.Slides.Add(2, 12)
Set PPSlide = PPPres.Slides.Add(3, 12)
pp.Visible = True
'Paste as text/table
'Title 1
PPPres.Slides(2).Select
Set Slide1Title = Sheets("presentation").Range("B2:G3")
Slide1Title.Copy
PPPres.Application.CommandBars.ExecuteMso ("PasteSourceFormatting")
'Here-ish is the error, after pasting in I can't seem to select it
pp.ActiveWindow.Selection.ShapeRange.Top = 10
pp.ActiveWindow.Selection.ShapeRange.Left = 75

Common problem. The most recently pasted shape will always be "on top" so you can use something like this to get at it:
PPPres.Slides(2).Shapes(PPPres.Slides(2).Shapes.Count)
Note that by selecting shapes and slides, you open yourself up to weird errors and slow down your code by an order of magnitude.
Dim oSh as Shape
With PPPres.Slides(2)
' Do the paste, then get a reference to the pasted shape:
Set oSh = PPPres.Slides(2).Shapes(PPPres.Slides(2).Shapes.Count)
With oSh
' set .Top, .Left etc
End With ' the shape
End With ' the slide

Temporary solution, when not using F8
Set Slide1Title = Sheets("presentation").Range("B2:G3")
Slide1Title.Copy
PPPres.Slides(2).Select
With PPPres.Slides(2)
PPPres.Application.CommandBars.ExecuteMso ("PasteSourceFormatting")
MsgBox ("1")
Set oSh = PPPres.Slides(2).Shapes(PPPres.Slides(2).Shapes.Count)
With oSh
pp.ActiveWindow.Selection.ShapeRange.Top = 10
pp.ActiveWindow.Selection.ShapeRange.Left = 75
End With ' the shape
End With ' the slide

Related

"This view does not support selection" error on VBA Excel when copy-pasting and resizing charts on Powerpoint slides

I am working on a project to automate powerpoint presentations. For this, I create charts on Excel and then I use VBA codes to copy and paste them on an existing powerpoint template.
However, when I try to resize and position my charts, I encounter the following problem: "Selection (unknown member) : Invalid request. This view does not support selection."
Here is my code :
Sub powerpoint()
Dim Powerpointapp As PowerPoint.Application
Dim PowerPointPrsn As PowerPoint.Presentation
Dim cht As ChartObject
Dim strpath As String
Dim rng As Range
Dim mySlide As Object
Dim myShape As Object
' paths to wkb and template
strpath = ThisWorkbook.Path & "\template.pptm"
' create new ppt with template
Set Powerpointapp = New PowerPoint.Application
Set PowerPointPrsn = Powerpointapp.Presentations.Open(strpath)
' copy paste chart 1 to slide 1 and resizing
Set cht = Worksheets("sheet name").ChartObjects(1)
cht.Copy
DoEvents
PowerPointPrsn.Slides(1).Shapes.PasteSpecial DataType:=ppPasteMetafilePicture
Powerpointapp.ActiveWindow.Selection.ShapeRange.Left = 50
Powerpointapp.ActiveWindow.Selection.ShapeRange.Top = 120
Powerpointapp.ActiveWindow.Selection.ShapeRange.Width = 590
My issue is starting at the line
Powerpointapp.ActiveWindow.Selection.ShapeRange.Left = 50
Thank you for your help !
As p77u77n77k mentioned, you haven't selected anything, so referring to the .Selection object will throw an error. You could instead:
At the top of the module, DIM oSh as Shape
Then
Set oSh = PowerPointPrsn.Slides(1).Shapes.PasteSpecial(ppPasteEnhancedMetafile)(1)
With oSh
.Left = 50
.Top = 120
.Width = 590
End with
Note that I've used ppPasteEnhancedMetafile rather than ppPasteMetafilePicture. I'm not sure what the diff is, but it seems to work better here. Microsoft's docs on this are all but useless. I suspect ppPasteMetafilePicture will give you a metafile rendition of whatever picture (ie raster image) is on the clipboard.

Exporting Excel tabs into Powerpoint Slides based on cell value

hope you can help.
Complete novice in VBA and I've searched and searched for an answer to my question but cannot find what I'm looking for.
What I have is a workbook with numerous tabs which hold different data and narrative. I need some way to to export these sheets (print view area) into separate PowerPoint slides only if cell "CI8" of those tabs are greater than 0. For example, I have tabs called "RestWest" "RestCentre" "RestEast" "RestRS" and if only RestWest and RestRS have their cell CI8 >0, only those two sheets would pull into Powerpoint. Pasting as a picture would be best.
I have been able to do it to export into PDF but senior management like PowerPoint better.
Hope you can help and give me some pointers.
Thanks for your time
Should have included the following code as it pulls a single sheet into PowerPoint but need to include the cell value criteria
Sub CopyRangeToPresentation()
Dim pp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim SlideTitle As String
Set pp = New PowerPoint.Application
Set PPPres = pp.Presentations.Add
pp.Visible = True
Set PPSlide = PPPres.Slides.Add(1, ppLayoutTitleOnly)
PPSlide.Select
Sheets("RestWest").Range("A1:CV77").CopyPicture _
Appearance:=xlScreen, Format:=xlPicture
PPSlide.Shapes.Paste.Select
pp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
pp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
SlideTitle = "My First PowerPoint Slide"
PPSlide.Shapes.Title.TextFrame.TextRange.Text = SlideTitle
pp.Activate
Set PPSlide = Nothing
Set PPPres = Nothing
Set pp = Nothing
End Sub
TheSpreadsheetGuru - Copy & Paste An Excel Range Into PowerPoint With VBA
I'm very novice also, but check out this link and maybe try something like:
For Each sht In Worksheets
With sht
If .Range("CI8").Value2 > 0 Then
.Range("Print_Area").Copy
'use SpreadSheetGuru to show you how to insert new slide and paste
' ...
End If
End With
Next

How to "Refresh Data" via VBA in Power Point?

so far I have tried the Chart.Refresh and Chart.Update and also ChartData.UpdateLinks and neither work.
My question is similar to this one only that this code did not work for my ppt
How to update excel embedded charts in powerpoint?
If i could Record Macro like in Excel the steps would be:
Select Chart
Chart Tools > Refresh Data
This is code is what I have managed to write but it fails at "gChart.Application.RefreshData":
Sub refreshchart()
Dim ppApp As PowerPoint.Application, sld As Slide
Dim s As PowerPoint.Shape
Dim gChart As Chart, i As Integer
ppApp.Visible = True
i = 3
Set sld = ActivePresentation.Slides(i)
sld.Select
For Each s In ActivePresentation.Slides(i)
If s.Type = msoEmbeddedOLEObject Then
Set gChart = s.OLEFormat.Object
With gChart.Application
gChart.Application.Refresh
Set gChart = Nothing
End If
Next s
End Sub
The Integer i is included to go from i=1 To 73, but as a test i am using Slide 3. Not all Slides have Charts but most of them have 4 Charts (65 out of 73).
I changed the code a little bit and with this little change, the refresh of the charts works again automatically.
Many times, if you share your excel ppt combo the links break and after restoring them the automated chart refresh doesn`t work.
With the downstanding macro the automated refresh will work again:
Sub REFRESH()
Dim pptChart As Chart
Dim pptChartData As ChartData
Dim pptWorkbook As Object
Dim sld As Slide
Dim shp As Shape
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.HasChart Then
Set pptChart = shp.Chart
Set pptChartData = pptChart.ChartData
pptChartData.Activate
shp.Chart.REFRESH
On Error Resume Next
On Error GoTo 0
End If
Next
Next
Set pptWorkbook = Nothing
Set pptChartData = Nothing
Set pptChart = Nothing
End Sub
The code below is in a macro in the Excel workbook which also contains the source data. Not sure if the code would be the same running it from PowerPoint. I simply open my Excel Workbook and then have it update the PowerPoint for me.
I have been looking forever to find an answer to this and finally managed to get it to work with a ton of reading and trial-and-error. My problem was that I have a PowerPoint with a lot of graphs that were created with CTRL+C and CTRL+V, so none of them are linked.
This is how I got it to work:
Dim myPresentation As PowerPoint.Presentation
Dim sld As PowerPoint.Slide
Dim shp As PowerPoint.Shape
Dim myChart As PowerPoint.Chart
For Each sld In myPresentation.Slides
For Each shp In sld.Shapes
If shp.HasChart Then
Set myChart = shp.Chart
myChart.ChartData.Activate
myChart.Refresh
End If
Next
Next
I don't know if there is unnecessary code in there but I am just happy that I finally got it to work so I'm not touching it anymore.
This code worked. But it works only if both files are open (the excel if its only one): The Power Point and the Excel with the data. It actually Refreshes all charts one by one.
Sub updatelinks()
Dim sld As Slide, shp As Shape
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
On Error Resume Next
shp.LinkFormat.Update
Next
Next
MsgBox ("Graficos actualizados con éxito")
End Sub
So, If the Excel is on a shared location, the code wont work because it takes too much time to retrieve the data. Im still looking for a way to do this. Thanks!
This may help, It opens and closes the embedded Excel object
For Each s In ActivePresentation.Slides(i)
If s.Type = msoEmbeddedOLEObject Then
s.Select 'select the object
s.OLEFormat.Activate 'Activate it (like 2x click))
ActiveWindow.Selection.Unselect 'To let it close
ActiveWindow.View.GotoSlide s.Slideindex 'make current slide active
End If
Next s

Pasting Excel Chart into PowerPoint Slide

The below Sub is supposed to paste an Excel chart into a newly created PowerPoint slide. It then exports the chart as a PNG:
Sub ChartsToPowerPoint()
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSlide As PowerPoint.Slide
'Open PowerPoint and create an invisible new presentation.
Set pptApp = New PowerPoint.Application
Set pptPres = pptApp.Presentations.Add(msoFalse)
'Set the charts and copy them to a new ppt slide
Set objChart = Worksheets("Sheet1").ChartObjects("Chart 1").Chart
objChart.ChartArea.Copy
Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
pptSlide.Shapes.PasteSpecial DataType:=ppPasteDefault, Link:=msoFalse
'Save Images as png
path = "C:\Users\xyz\Desktop\"
For j = 1 To pptSlide.Shapes.Count
With pptSlide.Shapes(j)
.Export path & j & ".png", ppShapeFormatPNG
End With
Next j
pptApp.Quit
Set pptSlide = Nothing
Set pptPres = Nothing
Set pptApp = Nothing
End Sub
I get a Run-time error:
Shapes (unknown member): Invalid request. Clipboard is empty or contains data which may not be pasted here.
At the line:
pptSlide.Shapes.PasteSpecial DataType:=ppPasteDefault, Link:=msoFalse
Error http://im64.gulfup.com/pZNwxJ.png
I tried pptSlide.Shapes.Paste but it gives the same error.
When I amend pptApp.Presentations.Add(msoFalse) to pptApp.Presentations.Add only it works but the PowerPoint App is displayed.
When I change to .PasteSpecial DataType:=ppPasteEnhancedMetafile or .PasteSpecial DataType:=ppPastePNG everything runs smoothly even with .Add(msoFalse).
I am thinking it might be something to do with setting the focus or so.
PasteSpecial and CommandBars.ExecuteMso should both work (tested your code in Excel/PowerPoint 2010 with the following caveat:
When you add presentation, you have to open it WithWindow:=True
Set pptPres = pptApp.Presentations.Add(msoCTrue)
I did some more digging, you need to use the CopyPicture method and then I think you can open withwindow=False. Try:
Sub ChartsToPowerPoint()
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSlide As PowerPoint.Slide
Dim objChart As Chart
'Open PowerPoint and create an invisible new presentation.
Set pptApp = New PowerPoint.Application
Set pptPres = pptApp.Presentations.Add(msoFalse)
Set objChart = Worksheets("Sheet1").ChartObjects("Chart 1").Chart
objChart.CopyPicture
Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
pptSlide.Shapes.PasteSpecial DataType:=ppPasteDefault, Link:=msoFalse
'Save Images as png
Path = CreateObject("Wscript.Shell").SpecialFolders("Desktop") & "\"
For j = 1 To pptSlide.Shapes.Count
With pptSlide.Shapes(j)
.Export Path & j & ".png", ppShapeFormatPNG
End With
Next j
pptApp.Quit
Set pptSlide = Nothing
Set pptPres = Nothing
Set pptApp = Nothing
End Sub
This is a common error we can experience when we copy information from one Office application to another. The best way I can describe it is that the program runs too fast and the information we copy never actually makes it into our clipboard.
This means that when we go and try to paste it, we get an error because there is nothing in our clipboard to paste.
Now lucky for us there is a way to fix this error but it requires us to add an extra line of code.
'Copy the chart
Set objChart = Worksheets("Sheet1").ChartObjects("Chart 1").Chart
objChart.CopyPicture
'Pause the application for ONE SECOND
Application.Wait Now + #12:00:01 AM#
'Paste your content into a slide as the "Default Data Type"
Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
pptSlide.Shapes.PasteSpecial DataType:=ppPasteDefault, Link:=msoFalse
Now all I did was add an extra line of code that pauses your Excel application for one second. This will give it enough time to make sure that the information is stored in your clipboard.
You might be asking the question why does this happen sometimes but then not other times. Well, it just boils down to this the clipboard can act unpredictably and clear out information inside of it.
That's why if we can avoid storing the information in the clipboard we try to. However, in this case, we can't avoid it so we just have to live with the unpredictability.
#areed1192's answer might work if PowerPoint had an Application.Wait message, but that's an Excel thing.
I was able to do something similar by using a technique found here:
Which is to say, put a the top of the module:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
And then call it like this:
Sleep 1000
DoEvents
(I'm not positive the DoEvents helped, but it seemed like it might be a good idea for resolving a race condition in VBA if that's what's going on.)
sld.Shapes.PasteSpecial DataType:=0
or
sld.Shapes.PasteSpecial DataType:=ppPasteShape

Excel to PowerPoint PasteSpecial and Keep Source Formatting

I'm trying to copy and paste a range from an Excel document into a PowerPoint slide.
It is copying the range as an image rather than keeping source formatting.
oPPTApp As PowerPoint.Application
Dim oPPTFile As PowerPoint.Presentation
Dim oPPTShape As PowerPoint.Shape
Dim oPPTSlide As PowerPoint.Slide
On Error Resume Next
Set XLApp = GetObject(, "Excel.Application")
On Error GoTo 0
Windows("File1.xlsx").Activate
Sheets("Sheet1").Select
Range("B3:N9").Select
Selection.Copy
oPPTApp.ActiveWindow.View.GotoSlide (2)
oPPTApp.ActiveWindow.Panes(2).Activate
oPPTApp.ActiveWindow.View.PasteSpecial DataType:=ppPasteOLEObject
oPPTApp.ActiveWindow.Selection.ShapeRange.Left = 35
oPPTApp.ActiveWindow.Selection.ShapeRange.Top = 150
Let’s break this problem into a few different parts:
Creating the PowerPoint Application
Copying the Charts Pasting the
Charts as the right format.
Now looking at your code, you are pretty much good to go on the first two. It’s pasting the object that is causing the problem. Let’s explore the different ways to paste.
USING THE EXECUTEMSO METHOD:
When we use this method it’s like we are right-clicking on the slide and pasting the object on to the slide. Now while this method is a completely valid way to paste, achieving this in VBA can be a little challenging. The reason why is because it is extremely volatile, and we must slow down our script to a snail’s pace!
To implement this method along with any of its different options, do the following:
'Create a new slide in the Presentation, set the layout to blank, and paste range on to the newly added slide.
Set PPTSlide = PPTPres.Slides.Add(1, ppLayoutBlank)
'WARNING THIS METHOD IS VERY VOLATILE, PAUSE THE APPLICATION TO SELECT THE SLIDE
For i = 1 To 5000: DoEvents: Next
PPTSlide.Select
'WARNING THIS METHOD IS VERY VOLATILE, PAUSE THE APPLICATION TO PASTE THE OBJECT
For i = 1 To 5000: DoEvents: Next
PPTApp.CommandBars.ExecuteMso "PasteSourceFormatting"
PPTApp.CommandBars.ReleaseFocus
'PASTE USING THE EXCUTEMSO METHOD - VERY VOLATILE
'Paste As Source Formatting
'PPTApp.CommandBars.ExecuteMso "PasteSourceFormatting"
'Paste as Destination Theme
'PPTApp.CommandBars.ExecuteMso "PasteDestinationTheme"
'Paste as Embedded Object
'PPTApp.CommandBars.ExecuteMso "PasteAsEmbedded"
'Paste Excel Table Source Formatting
'PPTApp.CommandBars.ExecuteMso "PasteExcelTableSourceFormatting"
'Paste Excel Table Destination Theme
'PPTApp.CommandBars.ExecuteMso "PasteExcelTableDestinationTableStyle"
Now if you look at my code, I had to pause it two different times to make sure it would work. This is because VBA will move way too fast otherwise and all that will happen is it will paste all the objects on the first slide! If we are only doing one paste we are usually safe without putting in the pauses, but the minute you want to go to a new slide put the pauses in!
USING THE REGULAR PASTE METHOD:
When we use this method, it’s like we are pressing Crtl+V and it will simply paste the object as a regular shape in PowerPoint. The regular shape means the default paste type in PowerPoint. Here is how we can implement a simple paste method:
'PASTE USING PASTE METHOD - NOT AS VOLATILE
'Use Paste method to Paste as Chart Object in PowerPoint
PPTSlide.Shapes.Paste
USING THE PASTE SPECIAL METHOD:
When we use this method it’s like we are pressing Ctrl+Alt+V on the keyboard and we get all sorts of different options of how to paste it. It ranges from a picture all the way to an embedded object that we can link back to the source workbook.
With the paste special method, sometimes we will still have to pause our scripts. The reason why is like the reason I mentioned above, VBA is volatile. Just because we copy it doesn’t mean it will make it to our clipboard. This problem can pop up and then disappear at the same time, so our best bet is to have a pause in our script to give VBA enough time to put the information in the clipboard. It usually doesn’t have to be a long pause but only a second or 2. Here is how we implement the paste special method with the different options we can use:
'PASTE USING PASTESPECIAL METHOD - NOT AS VOLATILE
'Paste as Bitmap
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteBitmap
'Paste as Default
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteDefault
'Paste as EnhancedMetafile
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteEnhancedMetafile
'Paste as HTML - DOES NOT WORK WITH CHARTS
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteHTML
'Paste as GIF
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteGIF
'Paste as JPG
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteJPG
'Paste as MetafilePicture
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteMetafilePicture
'Paste as PNG
PPTSlide.Shapes.PasteSpecial DataType:=ppPastePNG
'Paste as Shape
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteShape
'Paste as Shape, display it as an icon, change the icon label, and make it a linked icon.
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteShape, DisplayAsIcon:=True, IconLabel:="Link to my Chart", Link:=msoTrue
'Paste as OLEObject and it is linked.
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteOLEObject, Link:=msoFalse
With all that being said, if you paste an object as an OLEObject with a link most of the time the formatting comes over with it. Unless you have a special theme that only exist in Excel, that’s where you get into trouble. I ran into this problem when I was taking a chart from Excel To Word, but the Excel chart had a custom theme.
Here is your code, rewritten so that it will paste an object using the source format and setting the dimensions of it. I hope you don't mind me readjusting some of your code to make it a little more concise.
Sub PasteRangeIntoPowerPoint()
'Declare your variables
Dim oPPTApp As PowerPoint.Application
Dim oPPTFile As PowerPoint.Presentation
Dim oPPTShape As PowerPoint.Shape
Dim oPPTSlide As PowerPoint.Slide
Dim Rng As Range
'Get the PowerPoint Application, I am assuming it's already open.
Set oPPTApp = GetObject(, "PowerPoint.Application")
'Set a reference to the range you want to copy, and then copy it.
Set Rng = Worksheets("Sheet1").Range("B3:N9")
Rng.Copy
'Set a reference to the active presentation.
Set oPPTFile = oPPTApp.ActivePresentation
'Set a reference to the slide you want to paste it on.
Set oPPTSlide = oPPTFile.Slides(3)
'WARNING THIS METHOD IS VERY VOLATILE, PAUSE THE APPLICATION TO SELECT THE SLIDE
For i = 1 To 5000: DoEvents: Next
oPPTSlide.Select
'WARNING THIS METHOD IS VERY VOLATILE, PAUSE THE APPLICATION TO PASTE THE OBJECT
For i = 1 To 5000: DoEvents: Next
oPPTApp.CommandBars.ExecuteMso "PasteSourceFormatting"
oPPTApp.CommandBars.ReleaseFocus
For i = 1 To 5000: DoEvents: Next
'Set the dimensions of your shape.
With oPPTApp.ActiveWindow.Selection.ShapeRange
.Left = 35
.Top = 150
End With
End Sub
For that case, I have always been happy using Copy picture in Excel. To get it, click the arrow next to Copy.
In VBA, it translates to
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
In older versions of Excel (2003 and previous) you need to click Shift+Edit to get that option.
Have you tried using
oPPTApp.ActiveWindow.View.PasteSpecial DataType:=ppPasteDefault
Try this solution instead of using the Shapes.PasteSpecial method:
https://stackoverflow.com/a/19187572/1467082
PPTApp.CommandBars.ExecuteMso "PasteExcelChartSourceFormatting"
This does not create a link to the Excel document, it embeds a local copy of the document in the PowerPoint Presentation. I think I understand this is your requirement.
This is a code of mine that Keeps Source Formatting:
Sub SigAcc()
Application.ScreenUpdating = False
Dim myPresentation As Object
Set myPresentation = CreateObject("PowerPoint.Application")
Dim PowerPointApp As Object
Dim PPTApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Set objPPApp = New PowerPoint.Application
Set PPSlide = myPresentation.ActivePresentation.Slides(2)
lastrow = ThisWorkbook.Worksheets("The worksheet you would like to copy").Range("Letter of longest column (E.I. "A")" & Rows.Count).End(xlUp).Row
For p = PPSlide.Shapes.Count To 1 Step -1
Set myShape = PPSlide.Shapes(p)
If myShape.Type = msoPicture Then myShape.Delete
Next
Set myPresentation = myPresentation.ActivePresentation
Set mySlide = myPresentation.Slides(2)
On Error Resume Next
'assigning range into variable
Set r = ThisWorkbook.Worksheets("Sheet to copy").Range("A1:C" & lastrow)
On Error Resume Next
'If we have already opened powerpoint
Set PowerPointApp = GetObject(Class:="PowerPoint.Application")
'If Powerpoint is not opened
If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(Class:="Powerpoint.Application")
r.Copy
'to paste range
PPApp.CommandBars.ExecuteMso ("PasteSourceFormatting")
mySlide.Shapes.PasteSpecial
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
'Set position:
myShape.left = ActivePresentation.PageSetup.SlideWidth / 2 - ActivePresentation.PageSetup.SlideWidth / 2
myShape.Top = 80
PowerPointApp.Visible = True
PowerPointApp.Activate
'to clear the cutcopymode from clipboard
Application.CutCopyMode = False
End Sub
I had the same problem and was very annoyed that Excel wouldn't allow pasting the table with the CommandBars method. Moreover, none of the suggested solutions seemed to work for me.
Now, I have found a satisfying solution using the GetPastedShape function from Jamie Garroch, found here. At least it is applicable in cases where you already have a formatted table in place and only need to update the values.
The basic idea is to paste the table unformatted (which works fine), copy every single cell from the unformatted table to the desired formatted table that is already in place in your PPT. Afterwards the unformatted table is deleted. So when you're running the code you won't take notice that there was a temporary "support" table in your PPT.
Applied to your code the solution looks as follows:
Windows("File1.xlsx").Activate
Sheets("Sheet1").Select
Range("B3:N9").Select
Selection.Copy
ActivePresentation.Slides(1).Shapes.Paste
wb.Close SaveChanges:=False
Tab_unformatted = GetPastedShape.Name ' this is the Function from Jamie Garroch
For i = 1 To UBound(Tab_formatted, 1)
For j = 1 To UBound(Tab_formatted, 2)
ActivePresentation.Slides(1).Shapes("Tab_formatted").Table.Cell(i, j).Shape.TextFrame.TextRange.Text = _
ActivePresentation.Slides(1).Shapes(Tab_unformatted).Table.Cell(i, j).Shape.TextFrame.TextRange.Text
Next
Next
ActivePresentation.Slides(1).Shapes(Tab_unformatted).Delete

Resources