Sub Export_Allcahrts_ppt()
Dim mypowerpoint As PowerPoint.Application
Set mypowerpoint = New PowerPoint.Application
mypowerpoint.Visible = msoTrue
Dim mypowerpoint_pres As PowerPoint.Presentation
Set mypowerpoint_pres = mypowerpoint.Presentations.Add
Dim myslide As PowerPoint.Slide
Set myslide = mypowerpoint_pres.Slides.Add(1, ppLayoutBlank)
Dim mychart As ChartObject
Dim j As Long
j = 0
For Each mychart In Sheet1.ChartObjects
j = j + 1
Next
For Each mychart In Sheet1.ChartObjects
mychart.Copy
myslide.Shapes.PasteSpecial ppPasteBitmap
myslide.Shapes(1).Top = 100
myslide.Shapes(1).Height = 200
myslide.Shapes(1).Left = 30
If mypowerpoint_pres.Slides.Count < j Then
Set myslide = mypowerpoint_pres.Slides.Add(mypowerpoint_pres.Slides.Count + 1, ppLayoutBlank)
Else
Exit Sub
End If
Next
End Sub
First, you don't need to loop to get j; just use
j = Sheet1.ChartObjects.Count
But you also don't need j at all. What your code does is insert a new slide for each new chart if the number of slides does not yet equal the number of charts copied so far.
So try this slightly rearranged and streamlined code. I haven't tested it, but I don't think I've changed the syntax.
Sub Export_Allcahrts_ppt()
Dim mypowerpoint As PowerPoint.Application
Dim mypowerpoint_pres As PowerPoint.Presentation
Dim myslide As PowerPoint.Slide
Dim mychart As ChartObject
Dim j As Long
Set mypowerpoint = New PowerPoint.Application
mypowerpoint.Visible = msoTrue
Set mypowerpoint_pres = mypowerpoint.Presentations.Add
Set myslide = mypowerpoint_pres.Slides.Add(1, ppLayoutBlank)
j = Sheet1.ChartObjects.Count
For Each mychart In Sheet1.ChartObjects
mychart.Copy
myslide.Shapes.PasteSpecial ppPasteBitmap
With myslide.Shapes(myslide.Shapes.Count)
.Top = 100
.Height = 200
.Left = 30
End With
Next
End Sub
Related
Id like to be able to change the font size of the datalabels to size 14 while keeping the original text color formatting (some is white, some is black). Anyone have an idea of how I can accomplish this?
Dim PPTPres As PowerPoint.Presentation
Dim PPTSlide As PowerPoint.Slide
Dim PPTShape As PowerPoint.Shape
Dim SldIndex As Integer
Dim Chrt As ChartObject
Set PPTApp = New PowerPoint.Application
PPTApp.Visible = True
Set PPTPres = PPTApp.Presentations.Add
SldIndex = 1
For Each Chrt In ActiveSheet.ChartObjects
Chrt.Copy
Set PPTSlide = PPTPres.Slides.Add(SldIndex, ppLayoutBlank)
PPTSlide.Shapes.Paste
With PPTPres.Slides(SldIndex).Shapes("Chart 1")
.Top = 150
.Left = 350
.Height = 250
.Width = 350
.Chart.ChartArea.Border.LineStyle = xlContinuous
.Chart.ChartTitle.Format.TextFrame2.TextRange.Font.Size = 20
End With
SldIndex = SldIndex + 1
Next Chrt
End Sub```
I am trying to make the exported charts larger on the powerpoint slide, but keep running into issues with the loop. Any ideas?
Sub Export_Worksheet()
Dim PPTApp As PowerPoint.Application
Dim PPTPres As PowerPoint.Presentation
Dim PPTSlide As PowerPoint.Slide
Dim PPTShape As PowerPoint.Shape
Dim SldIndex As Integer
Dim Chrt As ChartObject
Set PPTApp = New PowerPoint.Application
PPTApp.Visible = True
Set PPTPres = PPTApp.Presentations.Add
SldIndex = 1
For Each Chrt In ActiveSheet.ChartObjects
Chrt.Copy
Set PPTSlide = PPTPres.Slides.Add(SldIndex, ppLayoutBlank)
PPTSlide.Shapes.Paste
SldIndex = SldIndex + 1
Next Chrt
End Sub
Check this code. It's works, if you need copy one chart on one slide, change size and position of chart and then repeat action for next slides
Sub Export_Worksheet()
Dim PPTApp As PowerPoint.Application
Dim PPTPres As PowerPoint.Presentation
Dim PPTSlide As PowerPoint.Slide
Dim PPTShape As PowerPoint.Shape
Dim SldIndex As Integer
Dim Chrt As ChartObject
Set PPTApp = New PowerPoint.Application
PPTApp.Visible = True
Set PPTPres = PPTApp.Presentations.Add
SldIndex = 1
For Each Chrt In ActiveSheet.ChartObjects
Chrt.Copy
Set PPTSlide = PPTPres.Slides.Add(SldIndex, ppLayoutBlank)
PPTSlide.Shapes.Paste
With PPTPres.Slides(SldIndex).Shapes("Chart 1")
.Top = 170
.Left = 530
.Height = 250
.Width = 400
End With
SldIndex = SldIndex + 1
Next Chrt
End Sub
I have a script which transfer all my charts to PPT perfectly but the issue is all chart paste in different slides. I have small charts that mean in a single slide can be store 4 charts. Is there any kind of script which arrange the charts as well in PPT slide and paste at least 4 charts in single slide of PPT.
Currently I'm using the below code
Sub Chart_TRF()
Dim PApp As PowerPoint.Application
Dim PPres As PowerPoint.Presentation
Dim PSlide As PowerPoint.Slide
Dim slide_index As Integer
Dim Chrt As ChartObject
Set PApp = New PowerPoint.Application
PApp.Visible = True
Set PPres = PApp.Presentations.Add
slide_index = 1
For Each Chrt In ActiveSheet.ChartObjects
Chrt.Copy
Set PSlide = PPres.Slides.Add(slide_index, ppLayoutBlank)
PSlide.Shapes.Paste
slide_index = slide_index + 1
Next Chrt
MsgBox ("PPT is created for all Charts")
End Sub
Please let me know if you have any query on this.
Thanks
I have amended your code so that each slide will contain 4 charts. You can change the starting left and top positions, along with the gap between charts, as desired.
Sub Chart_TRF()
Const START_LEFT_POS As Long = 20 'change the starting left position as desired
Const START_TOP_POS As Long = 20 'change the starting top position as desired
Const GAP As Long = 30 'change the gap between charts as desired
Dim PApp As PowerPoint.Application
Dim PPres As PowerPoint.Presentation
Dim PSlide As PowerPoint.Slide
Dim PShape As PowerPoint.Shape
Dim slide_index As Integer
Dim chart_index As Integer
Dim left_pos As Integer
Dim top_pos As Integer
Dim Chrt As ChartObject
Set PApp = New PowerPoint.Application
PApp.Visible = True
Set PPres = PApp.Presentations.Add
slide_index = 0
chart_index = 0
left_pos = START_LEFT_POS
top_pos = START_TOP_POS
For Each Chrt In ActiveSheet.ChartObjects
chart_index = chart_index + 1
If chart_index Mod 4 = 1 Then
slide_index = slide_index + 1
Set PSlide = PPres.Slides.Add(slide_index, ppLayoutBlank)
top_pos = START_TOP_POS
End If
Chrt.Copy
Set PShape = PSlide.Shapes.Paste(1)
If chart_index Mod 2 = 1 Then
With PShape
.Left = left_pos
.Top = top_pos
left_pos = left_pos + .Width + GAP
End With
Else
With PShape
.Left = left_pos
.Top = top_pos
left_pos = START_LEFT_POS
top_pos = top_pos + .Height + GAP
End With
End If
Next Chrt
MsgBox ("PPT is created for all Charts")
End Sub
EDIT
Here's the code that will copy the charts from all worksheets in the active workbook.
Sub Chart_TRF()
Const START_LEFT_POS As Long = 20 'change the starting left position as desired
Const START_TOP_POS As Long = 20 'change the starting top position as desired
Const GAP As Long = 30 'change the gap between charts as desired
Dim PApp As PowerPoint.Application
Dim PPres As PowerPoint.Presentation
Dim PSlide As PowerPoint.Slide
Dim PShape As PowerPoint.Shape
Dim slide_index As Integer
Dim chart_index As Integer
Dim left_pos As Integer
Dim top_pos As Integer
Dim Chrt As ChartObject
Dim ws As Worksheet
Set PApp = New PowerPoint.Application
PApp.Visible = True
Set PPres = PApp.Presentations.Add
slide_index = 0
chart_index = 0
left_pos = START_LEFT_POS
top_pos = START_TOP_POS
For Each ws In ActiveWorkbook.Worksheets
For Each Chrt In ws.ChartObjects
chart_index = chart_index + 1
If chart_index Mod 4 = 1 Then
slide_index = slide_index + 1
Set PSlide = PPres.Slides.Add(slide_index, ppLayoutBlank)
top_pos = START_TOP_POS
End If
Chrt.Copy
Set PShape = PSlide.Shapes.Paste(1)
If chart_index Mod 2 = 1 Then
With PShape
.Left = left_pos
.Top = top_pos
left_pos = left_pos + .Width + GAP
End With
Else
With PShape
.Left = left_pos
.Top = top_pos
left_pos = START_LEFT_POS
top_pos = top_pos + .Height + GAP
End With
End If
Next Chrt
Next ws
MsgBox ("PPT is created for all Charts")
End Sub
I have an Excel with Macro which should:
toggle to active PPT
select slide "X" and delete graphs
Go to Tab "X" in excel
grab new Graph
Paste onto the "X" slide
repeat 5 times
here is the code I've compiled so far:
Dim PPT As Object
Dim rng As Object
Dim rng1 As Object
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object
Dim ActivePresentation As Object
'Copy Range from Excel
Set rng = Sheet3.ChartObjects("Chart 6")
Set rng1 = Sheet3.ChartObjects("Chart 7")
Set rng2 = Sheet3.ChartObjects("Chart 8")
Set PPT = CreateObject("PowerPoint.Application")
With PPT
.Visible = True
.WindowState = 1
.Activate
End With
'Is PowerPoint already opened?
Set PowerPointApp = GetObject(class:="PowerPoint.Application")
Set myPresentation = PowerPointApp.Presentations.Add *this should not say add as it adds a slide,but no luck with any other commands*
' PowerPointApp.Presentations.Add
Set mySlide = myPresentation.Slides.Add(1, 11) *this should not say add as it adds a slide,but no luck with any other commands*
'Copy Excel Range
rng.Copy
'Paste to PowerPoint and position
mySlide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
'Set position:
myShape.Left = 20
myShape.Top = 152
rng1.Copy
mySlide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
myShape.Left = 486
myShape.Top = 152
Set mySlide = myPresentation.Slides.Add(2, 11) '11 = ppLayoutTitleOnly
etc..
End Sub
this creates a new PPT and add slides to the new ppt, have tried a numerous helps and web pages but unfortunately was not able to find a piece of code which would tackle this problem. Would be much appreciated if you could advise or point me to the correct help or tutorial which would be possible to solve this issue with.
code is based on the following assumptions from your statement
Already have a presentation open
want to copy two or three charts from each sheets, starting from Sheets(2) to Sheets(5) to slides 2 to 5 respectively as shown below.
Code may be modified to your requirement
Sub AddtoOpenPPT()
Dim PPT As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Dim mySlide As PowerPoint.Slide
Dim myShape As PowerPoint.Shape
Dim Fname As String
Dim sld As Long, i As Long, ObjNo As Long
Dim Rng(0 To 9) As Object
Set Rng(0) = Sheet3.ChartObjects("Chart 6")
Set Rng(1) = Sheet3.ChartObjects("Chart 7")
Set Rng(2) = Sheet3.ChartObjects("Chart 8")
Set Rng(3) = Sheet3.ChartObjects("Chart 5")
Set Rng(4) = Sheet1.Range("b4:j14")
Set Rng(5) = Sheet1.Range("A4:l4", "A15:j19")
Set Rng(6) = Sheet4.ChartObjects("Chart 13")
Set Rng(7) = Sheet4.ChartObjects("Chart 15")
Set Rng(8) = Sheet4.ChartObjects("Chart 17")
Set Rng(9) = Sheet4.ChartObjects("Chart 19")
Set PPT = GetObject(class:="PowerPoint.Application")
Set myPresentation = PPT.ActivePresentation
ObjNo = 0
For sld = 2 To 5
Set mySlide = myPresentation.Slides(sld)
For i = mySlide.Shapes.Count To 1 Step -1
mySlide.Shapes(i).Delete
Next
For i = 1 To 3
Rng(ObjNo).Copy
mySlide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
myShape.Left = IIf(i Mod 2 = 1, 20, 486)
myShape.Top = IIf(i < 3, 50, 200)
ObjNo = ObjNo + 1
If ObjNo > UBound(Rng) Then Exit For
Next
If ObjNo > UBound(Rng) Then Exit For
Next sld
End Sub
This is the code I wrote to copy over a picture from Excel to PowerPoint. I have other code that preps the PowerPoint slide, which should have no factor on this. For some reason this code is not working. It is giving me the error that no slide is currently in view. Thanks in advance for the help.
Sub CopyPicToPPt()
Dim pptApp As PowerPoint.Application
Dim pptPresent As Presentation
Dim sldPPT As Slide
Dim shpPic As Shape
Dim oLayout As CustomLayout
Dim x As PowerPoint.Shape
ActiveWorkbook.Sheets("Sheet1").Select
Set shpPic = Sheet4.Shapes("Picture 3") '<< --- Pic Name
shpPic.CopyPicture
Set pptApp = GetObject(class:="PowerPoint.Application")
pptApp.Visible = True
pptApp.Activate
Set pptPresent = pptApp.ActivePresentation
Set sldPPT = pptApp.ActiveWindow.View.Slide
sldPPT.Shapes.PasteSpecial(ppPasteMetafilePicture).Select
pptApp.ActiveWindow.Selection.ShapeRange.LockAspectRatio = False
pptApp.ActiveWindow.Selection.ShapeRange.Left = 24
pptApp.ActiveWindow.Selection.ShapeRange.Top = 6
pptApp.ActiveWindow.Selection.ShapeRange.Height = 55
pptApp.ActiveWindow.Selection.ShapeRange.width = 672
End Sub
After a little fiddling and some help from a friend I think I have it! - Cheers
Sub CopyPicToPPt()
Dim pptApp As PowerPoint.Application
Dim pptPresent As Presentation
Dim sldPPT As Slide
Dim shpPic As Shape
Dim oLayout As CustomLayout
Dim x As PowerPoint.Shape
ActiveWorkbook.Sheets("Sheet1").Visible = True
ActiveWorkbook.Sheets("Sheet1").Select
Set shpPic = Sheet4.Shapes("Picture 3") '<< --- Pic Name
shpPic.CopyPicture
Set pptApp = GetObject(class:="PowerPoint.Application")
pptApp.Visible = True
pptApp.Activate
pptApp.ActivePresentation.Slides(1).Select
Set pptPresent = pptApp.ActivePresentation
Set sldPPT = pptApp.ActivePresentation.Slides(1)
sldPPT.Shapes.PasteSpecial(ppPasteMetafilePicture).Select
pptApp.ActiveWindow.Selection.ShapeRange.LockAspectRatio = False
pptApp.ActiveWindow.Selection.ShapeRange.Left = 24
pptApp.ActiveWindow.Selection.ShapeRange.Top = 6
pptApp.ActiveWindow.Selection.ShapeRange.Height = 55
pptApp.ActiveWindow.Selection.ShapeRange.width = 672
ActiveWorkbook.Sheets("Sheet1").Visible = False
End Sub