mjohnston0209
Board Regular
- Joined
- Nov 6, 2017
- Messages
- 55
I am working on a macro that will transfer several objects from a specific sheet (sheet2) in excel and onto a specific slide in PowerPoint. I watched a very informative video on how to do this. The objects copied in the video were a range, table, and chart. This all works for me. However, I also need to copy pivot tables.
Below is the code I am currently using. I need to add pivot table coding to two areas. One is under 'Create array to house objects we want to export' and the other is under 'Depending on the object type, copy it a certain way'.
Any help would be greatly appreciated!
Below is the code I am currently using. I need to add pivot table coding to two areas. One is under 'Create array to house objects we want to export' and the other is under 'Depending on the object type, copy it a certain way'.
Any help would be greatly appreciated!
VBA Code:
Sub CopyObject()
'Declare PowerPoint Variables
Dim PPTApp As PowerPoint.Application
Dim PPTPres As PowerPoint.Presentation
Dim PPTSlide As PowerPoint.Slide
Dim PPTShape As PowerPoint.Shape
'Declare Excel Variables
Dim ExcObj, ObjType, ObjArray As Variant
Dim LefArray, TopArray, HgtArray, WidArray As Variant
Dim x As Integer
'Open Instance of PowerPoint
Set PPTApp = New PowerPoint.Application
PPTApp.Visible = True
PPTApp.Presentations.Open "C:\Users\mjohnston\Desktop\Presentation1.pptx"
PPTApp.Activate
'Create a reference to the slide we want to work with and delete all shapes on slide
Set PPTSlide = PPTApp.ActivePresentation.Slides(2)
TotalShapes = PPTSlide.Shapes.Count
For i = TotalShapes To 1 Step -1
If PPTSlide.Shapes(i).Type <> msoTextBox Then PPTSlide.Shapes(i).Delete
Next i
'Create array to house objects we want to export
ObjArray = Array(Sheet2.Range("B2:D5"), Sheet2.ChartObjects(1), Sheet2.ListObjects(1))
'Define my dimesnion arrays
LefArray = Array(59.3)
TopArray = Array(270)
HgtArray = Array(105.27)
WidArray = Array(359.23)
'Loop through the object array and copy each object
For x = LBound(ObjArray) To UBound(ObjArray)
'Determine Object Type
ObjType = TypeName(ObjArray(x))
'Depending on the object type, copy it a certain way
Select Case ObjType
Case "Range"
Set ExcObj = ObjArray(x)
ExcObj.Copy
Case "ChartObject"
Set ExcObj = ObjArray(x)
ExcObj.Chart.ChartArea.Copy
Case "ListObject"
Set ExcObj = ObjArray(x)
ExcObj.Range.Copy
End Select
'Pause the Excel Application
Application.Wait Now() + #12:00:01 AM#
'Past the object in the slide
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteOleOjbect
'Set a reference to the shape
Set PPTShape = PPTSlide.Shapes(PPTSlide.Shapes.Count)
'Set the dimension of my shape
With PPTShape
.Left = LefArray(x)
.Height = HgtArray(x)
.Width = WidArray(x)
.Top = TopArray(x)
End With
Next
End Sub