I have a code in Excel VBA that will export both my charts and data tables to PowerPoint, but I can't seem to get them both on the same slides (so chart above data table in PowerPoint). Please help.
Sub ExportMultipleChartsToPowerPoint_FullWorkbook3()
'Declare PowerPoint Object Variables
Dim PPTApp As PowerPoint.Application
Dim PPTPres As PowerPoint.Presentation
Dim PPTSlide As PowerPoint.Slide
Dim PPTShape As PowerPoint.Shape
Dim PPTShapeRng As PowerPoint.ShapeRange
Dim ShpCnt As Integer
'Declare Excel Object Variables
Dim Chrt As ChartObject
Dim Wrksht As Worksheet
Dim SldIndex As Integer
Dim ExcRng As Range
Dim RngArray As Variant
'Create a new instance of PowerPoint
Set PPTApp = New PowerPoint.Application
PPTApp.Visible = True
'Create a new Presentation within the Application
Set PPTPres = PPTApp.Presentations.Add
'Create an Index Handler for slide creation
SldIndex = 1
'For Errors
On Error Resume Next
'Look through all of the Worksheets in the ACTIVE WORKBOOK
For Each Wrksht In Worksheets
'Loop through all the Chart Objects on the ACTIVESHEET
For Each Chrt In Wrksht.ChartObjects
'Copy the chart
Chrt.Copy
'Tell Macro to wait for ONE SECOND
Application.Wait Now + #12:00:01 AM#
'Create a new slide, set the layout to blank, and paste the chart
Set PPTSlide = PPTPres.Slides.Add(SldIndex, ppLayoutTitleOnly)
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteJPG
'Add Text to Slide Title and format
PPTSlide.Shapes(1).TextFrame.TextRange = "X008 - CARS all bookings consultant YR details"
'Count the number of shapes on my slide
ShpCnt = PPTSlide.Shapes.Count
'Set a reference to the shape I want to manipulate
Set PPTShapeRng = PPTSlide.Shapes.Range(Array(ShpCnt))
'Set Dimension of my shape range
With PPTShapeRng
.Height = 240
.Width = 660
.Top = 135
.Align msoAlignCenters, True
End With
'Increment our slide index
SldIndex = SldIndex + 1
Next Chrt
Next Wrksht
'Create an array that has the references to the ranges I want to export
RngArray = Array(Worksheets("Global Results").Range("A30:H37"), Worksheets("G-FPO-GC").Range("A30:H37"), Worksheets("G-FPO-GCA").Range("A30:H37"), Worksheets("G-FPO-GCE").Range("A30:H37"), Worksheets("G-FPO-GCG").Range("A30:H37"), Worksheets("G-FPO-GCN").Range("A30:H37"), Worksheets("G-FPO-GCO").Range("A30:H37"))
'Loop through this array, copy the range, and create a new slide, and then paste the range in the slide
For x = LBound(RngArray) To UBound(RngArray)
'Set a reference to the range we want to export
Set ExcRng = RngArray(x)
'Copy the range
ExcRng.Copy
'Create a new slide in the presentation
Set PPTSlide = PPTPres.Slides.Add(x + 1, ppLayoutTitleOnly)
'Paste the range in the slide
PPTApp.ActiveWindow.ViewType = ppViewNormal
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteEnhancedMetafile
'Count the number of shapes on my slide
ShpCnt = PPTSlide.Shapes.Count
'Set a reference to the shape I want to manipulate
Set PPTShapeRng = PPTSlide.Shapes.Range(Array(ShpCnt))
'Set the dimensions of my shaperange
With PPTShapeRng
.Align msoAlignCenters, True
.Left = 80
.Top = 430
.Height = 100
End With
Next x
End Sub
Every now and then, there will be another error message that says there's something wrong with my Chrt.Copy and I have no idea why
Related
I copy paste my ranges from XL to PPT.
It creates new slides and adds range as picture.
How can I make it paste range to existing slide, instead of creating slide?
Right now it loops and paste copied picture to new slide.
I get error in this line: "Set PPslide = ActiveWindow.Selection.SlideRange(1)"
Thanks.
Dim PP As PowerPoint.Application
Dim PPpres As PowerPoint.Presentation
Dim PPslide As Object
Dim k As Long, i As Long
Dim PpShape As PowerPoint.Shape
Dim SlideTitle As String
Dim SlideNum As Integer
Dim WSrow As Long
Dim Sh As Shape
Dim Rng As Range
Dim myShape As Object
'Open PowerPoint and create new presentation
Set PP = GetObject(class, "PowerPoint.Application")
PP.Visible = True
Set PPpres = PP.Presentations.Open(Filename:="C:\Users\Mac\Desktop\test\PPT.pptx")
k = 1
For i = 6 To Cells(70, Columns.Count).End(xlToLeft).Column Step 10
With Cells(70, i)
.Resize(1, 10).CopyPicture Appearance:=xlPrinter, Format:=xlPicture
DoEvents
DoEvents
.Offset(15, 0).PasteSpecial
DoEvents
DoEvents
End With
'Give the last pasted picture a name.
ActiveSheet.Pictures(ActiveSheet.Pictures.Count).Name = "Element" & k
' Here you're adding a new slide, which you've said you don't want.
' Comment it out:
Set PPslide = PPpres.Slides.Add(1, ppLayoutBlank)
' Assuming the active slide, no need to anywhere:
PP.ActiveWindow.View.GotoSlide (1)
' Change this to use the currently active slide
Set PPslide = PPpres.Slides(1)
Set PPslide = ActiveWindow.Selection.SlideRange(1) '<-- I get error here.
'Paste to PowerPoint and position
PPslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = PPslide.Shapes(PPslide.Shapes.Count)
'Set position:
myShape.Left = 40
myShape.Top = 180
myShape.Height = 220
myShape.Width = 850
'Make PowerPoint Visible and Active
PP.Visible = True
PP.Activate
'Clear The Clipboard
Application.CutCopyMode = False
Next i
k = k + 1
Assuming you want to paste into the currently active slide:
' Here you're adding a new slide, which you've said you don't want.
' Comment it out:
' Set PPslide = PPpres.Slides.Add(1, ppLayoutBlank)
' Assuming the active slide, no need to anywhere:
' PP.ActiveWindow.View.GotoSlide (1)
' Change this to use the currently active slide
' Set PPslide = PPpres.Slides(1)
Set PPslide = ActiveWindow.Selection.SlideRange(1)
'Paste to PowerPoint and position
PPslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = PPslide.Shapes(PPslide.Shapes.Count)
I am fairly new at this but am beating my head on this. I have a basic functioning code where it opens up the PP and inputs the data from my excel sheets. But it auto centers each sheet's array and the text looks very small. I want to be able to basically make the text larger on different scales, and reposition a couple arrays on each sheet as I see fit. I know the "populate our arrays" is kind of truncated, so I would assume I would need to break that apart to apply the custom dimensions to each sheet. Thank you so much in advance.
Sub ExportMultipleRangeToPowerPoint_Method1()
'Declare PowerPoint Variables
Dim PPTApp As PowerPoint.Application
Dim PPTPres As PowerPoint.Presentation
Dim PPTSlide As PowerPoint.Slide
'Opens a new PowerPoint presentation based on template and pastes data into Slide 2 of Powerpoint from Excel
Dim PPapp As PowerPoint.Application, PPpres As PowerPoint.Presentation, PPslide As PowerPoint.Slide, PPShape As Object
Dim XLws As Worksheet
'Declare Excel Variables
Dim ExcRng As Range
Dim RngArray As Variant
Dim ShtArray As Variant
'Populate our arrays
RngArray = Array("A1:E16", "C2:E6", "B2:D6")
ShtArray = Array("Summary", "Sheet2", "Sheet3")
'Create a new instance of PowerPoint
Set PPTApp = New PowerPoint.Application
PPTApp.Visible = True
'Create a new Presentation
Set PPTPres = PPTApp.Presentations.Add
'Loop through the range array, create a slide for each range, and copy that range on to the slide.
For x = LBound(RngArray) To UBound(RngArray)
'Set a reference to the range
Set ExcRng = Worksheets(ShtArray(x)).Range(RngArray(x))
'Copy the range
ExcRng.Copy
'Create a new Slide
Set PPTSlide = PPTPres.Slides.Add(x + 1, ppLayoutBlank)
'Paste the range in the slide
PPTSlide.Shapes.Paste
Next x
End Sub
Try
' width, height, left, top
ImgArray = Array("300,300,100,100", _
"200,200,200,200", _
"150,150,150,150")
'Create a new instance of PowerPoint
Set PPTApp = New PowerPoint.Application
PPTApp.Visible = True
'Create a new Presentation
Set PPTPres = PPTApp.Presentations.Add
'Loop through the range array, create a slide for each range, and copy that range on to the slide.
Dim x
For x = LBound(RngArray) To UBound(RngArray)
'Set a reference to the range
Set ExcRng = Worksheets(ShtArray(x)).Range(RngArray(x))
'Copy the range
ExcRng.Copy
'Create a new Slide
Set PPTSlide = PPTPres.Slides.Add(x + 1, ppLayoutBlank)
'Paste the range in the slide
ar = Split(ImgArray(x), ",")
With PPTSlide.Shapes.PasteSpecial(ppPasteOLEObject)
.Width = ar(0)
.Height = ar(1)
.Left = ar(2)
.Top = ar(3)
End With
Next x
Is there a way such that i can copy and paste multiple charts that are grouped in four as shown below from excel to my existing powerpoint slides 28 and slides 29? The name of the groups are group 16 for the left group, group 17 for the right group. I have tried to use Chrt.CopyPicture but it only copies charts separately to the slides instead of a group like the one outline on the 4 charts shown on the left side of the picture below. By the way, my only code only copies charts individually to slide 28.
Sub ExportChartsTopptSingleWorksheet()
'Declare PowerPoint Variables
Dim PPTApp As Object
Dim PPTPres As Object
Dim PPTShape As Object
Dim mySlide, myslide2 As Object
'Declare Excel Variables
Dim Chrt As ChartObject
If PPTApp Is Nothing Then _
Set PPTApp = CreateObject(class:="PowerPoint.Application")
On Error GoTo 0
PPTApp.Visible = True
'Create new presentation in the PowerPoint application.
Set PPTPres = PPTApp.Presentations.Open(Filename:="\\fab2crp-nas1\home22\kkang2\Profile\Desktop\myassignment3\mypresentationsample.pptx")
Set mySlide = PPTPres.Slides.Add(28, 1)
'Loop through all the CHARTOBJECTS in the ACTIVESHEET.
For Each Chrt In ActiveSheet.ChartObjects
'Copy the Chart
Chrt.CopyPicture '<------ method copy fail error here
'paste all the chart on to exisitng ppt slide 28
mySlide.Shapes.Paste
Next Chrt
End Sub
Currently, charts are copied individually to ppt slides
Expected
This worked for me.
Sub ExportChartsTopptSingleWorksheet()
Const PER_ROW As Long = 2 'charts per row in PPT
Const T_START As Long = 40 'start chart top
Const L_START As Long = 40 'start chart left
Dim PPTApp As Object
Dim PPTPres As Object
Dim PPTShape As Object
Dim mySlide, myslide2 As Object, i As Long
Dim Chrt As ChartObject, T As Long, L As Long
If PPTApp Is Nothing Then _
Set PPTApp = CreateObject(class:="PowerPoint.Application")
PPTApp.Visible = True
Set PPTPres = PPTApp.Presentations.Add()
Set mySlide = PPTPres.Slides.Add(1, 1)
i = 0
For Each Chrt In ActiveSheet.ChartObjects
Chrt.Chart.CopyPicture
i = i + 1
'work out the top/left values
T = T_START + (Application.Floor((i - 1) / PER_ROW, 1)) * Chrt.Height
L = L_START + ((i - 1) Mod PER_ROW) * Chrt.Width
With mySlide.Shapes
.Paste
.Item(.Count).Top = T
.Item(.Count).Left = L
End With
Next Chrt
End Sub
I am transferring data from excel to powerpoint slides with an automated script by using EXcel VBA. I'm trying to copy the usedrange of a excel worksheet and paste it to as a image in a powerpoint Template of 4th slide and from there on it should add new slides and copy the remaining worksheets to the next further slides.
The code which i'm currently using is getting the following error "öbject variable or with block variable not set"
Can anyone suggest me the code for the following.
Hope this is clearly explained. If not please ask for more clarification.
Thanks
Private Sub CommandButton2_Click()
Dim PP As PowerPoint.Application
Dim PPpres As PowerPoint.Presentation
Dim PPslide As Object
Dim PpShape As PowerPoint.Shape
Dim SlideTitle As String
Dim SlideNum As Integer
Dim WSrow As Long
Dim Sh As Shape
Dim Rng As Range
Dim myshape As Object
'Open PowerPoint and create new presentation
Set PP = GetObject(class, "PowerPoint.Application")
PP.Visible = True
PP.Presentations.Open FileName:=("\\C:\Users\Templates)"
'Specify the chart to copy and copy it
For Each WS In Worksheets
If (WS.Name) <> "EOS" Then
ThisWorkbook.Worksheets(WS.Name).Activate
ThisWorkbook.ActiveSheet.UsedRange.CopyPicture
'pSlide.Shapes.Paste
'Copy Range from Excel
Set Rng = ThisWorkbook.ActiveSheet.Range("A1:I8")
'Copy Excel Range
Rng.Copy
'Set PPslide = PPpres.Slides.Add(5, 33)
PP.ActiveWindow.View.GotoSlide (4)
Set PPslide = PPpres.Slides(4).Shapes.Paste
'Paste to PowerPoint and position
PPslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myshape = PPslide.Shapes(PPslide.Shapes.Count)
'Set position:
myshape.Left = 66
myshape.Top = 152
End If
Next
'Make PowerPoint Visible and Active
PowerPointApp.Visible = True
PowerPointApp.Activate
'Clear The Clipboard
Application.CutCopyMode = Falseenter code here`
End Sub
Try changing:
'Set PPslide = PPpres.Slides.Add(5, 33)
PP.ActiveWindow.View.GotoSlide (4)
Set PPslide = PPpres.Slides(4).Shapes.Paste '<< CHANGING THIS LINE ONLY
'Paste to PowerPoint and position
PPslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
To:
'Set PPslide = PPpres.Slides.Add(5, 33)
PP.ActiveWindow.View.GotoSlide (4)
Set PPslide = PPpres.Slides(4)
'Paste to PowerPoint and position
PPslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Also, as per my comment, you'll need to change the following last few lines of your code:
'Make PowerPoint Visible and Active
PowerPointApp.Visible = True
PowerPointApp.Activate
'Clear The Clipboard
Application.CutCopyMode = Falseenter code here`
End Sub
To:
'Make PowerPoint Visible and Active
PP.Visible = True
PP.Activate
'Clear The Clipboard
Application.CutCopyMode = False
'enter code here
End Sub
I need code snippet to copy all charts and tables from Excel file for all sheets to PowerPoint file using Macro (VBA) in Excel.
Below code copies only charts. I want to copy all tables and charts and images.
Sub PushChartsToPPT()
'Set reference to 'Microsoft PowerPoint 12.0 Object Library'
'in the VBE via Tools > References...
'
Dim ppt As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSld As PowerPoint.Slide
Dim pptCL As PowerPoint.CustomLayout
Dim pptShp As PowerPoint.Shape
Dim cht As Chart
Dim ws As Worksheet
Dim i As Long
'Get the PowerPoint Application object:
Set ppt = CreateObject("PowerPoint.Application")
ppt.Visible = msoTrue
Set pptPres = ppt.Presentations.Add
'Get a Custom Layout:
For Each pptCL In pptPres.SlideMaster.CustomLayouts
If pptCL.Name = "Title and Content" Then Exit For
Next pptCL
'Copy ALL charts in Chart Sheets:
For Each cht In ActiveWorkbook.Charts
Set pptSld = pptPres.Slides.AddSlide(pptPres.Slides.Count + 1, pptCL)
pptSld.Select
For Each pptShp In pptSld.Shapes.Placeholders
If pptShp.PlaceholderFormat.Type = ppPlaceholderObject Then Exit For
Next pptShp
If pptShp Is Nothing Then Stop
cht.ChartArea.Copy
ppt.Activate
pptShp.Select
ppt.Windows(1).View.Paste
Next cht
'Copy ALL charts embedded in EACH WorkSheet:
For Each ws In ActiveWorkbook.Worksheets
For i = 1 To ws.ChartObjects.Count
Set pptSld = pptPres.Slides.AddSlide(pptPres.Slides.Count + 1, pptCL)
pptSld.Select
For Each pptShp In pptSld.Shapes.Placeholders
If pptShp.PlaceholderFormat.Type = ppPlaceholderObject Then Exit For
Next pptShp
Set cht = ws.ChartObjects(i).Chart
cht.ChartArea.Copy
ppt.Activate
pptShp.Select
ppt.Windows(1).View.Paste
Next i
Next ws
End Sub
For pictures try using shapes from How to select pictures
Dim Pic As Shape
For Each Pic In ActiveSheet.Shapes
If Pic.Type = msoPicture Then
Pic.Select
'do something with image
End If
Next Pic