Optimizing VBA macro for PowerPoint - excel

I am creating a powerpoint from the VBA editor and when I create the individual slides, it works great. However, when I try to create them all at once, PowerPoint crashes. I clear memory by setting Application.CutCopyMode=False at the end of each slide and have Application.Wait for 7 seconds.
My powerpoint is going to be about 25 slides and its already crashing past slide 7. Usually it crashes when I am formatting. I have added in the 3 basic layouts for each Macro I use and slides 8 and 9 of where it crashes.
The First Macro I use copies a slide from last presentation and
pastes to new powerpoint.
The Second Pastes a Table
The Third Pastes a Table, Chart, and Picture (only slide with Picture, otherwise slides of this type paste a table and chart only).
Code:
Sub CreateNewPresentation()
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim slidesCount As Long
If ppApp Is Nothing Then
Set ppApp = New PowerPoint.Application
End If
Set ppPres = ppApp.Presentations.Add
ppPres.SaveAs "FileName"
ppApp.Visible = True
slidesCount = ppPres.Slides.Count
Call create_Slide1(slidesCount, ppPres, ppApp)
slidesCount = ppPres.Slides.Count
Application.CutCopyMode = False
Call create_Slide2(slidesCount, ppPres)
slidesCount = ppPres.Slides.Count
Application.CutCopyMode = False
Call create_Slide3(slidesCount, ppPres)
slidesCount = ppPres.Slides.Count
Application.CutCopyMode = False
ppPres.Save
ppPres.Close
Call create_Slide8(slidesCount, ppPres)
slidesCount = ppPres.Slides.Count
Application.CutCopyMode = False
Call create_Slide9(slidesCount, ppPres)
slidesCount = ppPres.Slides.Count
Application.CutCopyMode = False
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
sub Create_Slide1(sldNum As Long, ppPrez As PowerPoint.Presentation, ppt As PowerPoint.Application)
Dim myFile As String
Dim ppSlide As PowerPoint.Slide
Dim objPres As PowerPoint.Presentation
Set ppSlide = ppPrez.Slides.Add(sldNum + 1, ppLayoutBlank)
ppPrez.PageSetup.SlideSize = ppSlideSizeLetterPaper
myFile:"File name and path....."
Set objPres=ppt.Presentations.Open(myFile)
objPres.Slides(1).Copy
ppPrez.Slides.Paste Index:=sldNum+1
objPres.Close
ppPrez. Slides(sldNum+2).Delete
End Sub
Sub create_Slide2(sldNum As Long, ppPrez As PowerPoint.Presentation)
Dim ppSlide As PowerPoint.Slide
Set ppSlide = ppPrez.Slides.Add(sldNum + 1, ppLayoutBlank)
ppPrez.PageSetup.SlideSize = ppSlideSizeLetterPaper
ppSlide.Select
ThisWorkbook.Worksheets("Sheet2").Activate
ActiveSheet.Range(Cells(3, 2), Cells(27, 11)).Copy
ppSlide.Shapes.Paste.Select
With ppSlide.Shapes(1)
.Top = ppPrez.PageSetup.SlideHeight / 20
.Left = ppPrez.PageSetup.SlideWidth / 20
.Height = 17 * (ppPrez.PageSetup.SlideHeight) / 20
.Width = 9 * (ppPrez.PageSetup.SlideWidth / 10)
End With
End Sub
sub create_Slide3(sldNum As Long, ppPrez As PowerPoint.Presentation)
Dim ppSlide As PowerPoint.Slide
Dim ppTextBox As PowerPoint.Shape
Set ppSlide = ppPrez.Slides.Add(sldNum + 1, ppLayoutBlank)
ppPrez.PageSetup.SlideSize = ppSlideSizeLetterPaper
ppSlide.Select
Set ppTextBox = ppSlide.Shapes.AddTextbox( _
msoTextOrientationHorizontal, 0, 15, ppPrez.PageSetup.SlideWidth, 60)
With ppTextBox.TextFrame
.TextRange.Text = "Slide3"
.TextRange.ParagraphFormat.Alignment = ppAlignCenter
.TextRange.Font.Size = 20
.TextRange.Font.Name = "Calibri"
.VerticalAnchor = msoAnchorMiddle
End With
ThisWorkbook.Sheets("Sheet3").Activate
ActiveSheet.Range(Cells(17, 10), Cells(19, 19)).Copy
ppSlide.Shapes.Paste.Select
With ppSlide.Shapes(2)
.Width = (6 / 10) * ppPrez.PageSetup.SlideWidth
.Left = (1 / 40) * ppPrez.PageSetup.SlideWidth
.Top = (5 / 8) * ppPrez.PageSetup.SlideHeight
End With
Sheets("Sheet3").Shapes("Shape1").CopyPicture
ppSlide.Shapes.Paste
ppSlide.Shapes(4).Height = 850
ppSlide.Shapes(4).Width = 275
ppSlide.Shapes(4).Left = (6.2 / 10) * ppPrez.PageSetup.SlideWidth
ppSlide.Shapes(4).Top = (1 / 10) * ppPrez.PageSetup.SlideHeight
End sub
sub create_Slide8(sldNum As Long, ppPrez As PowerPoint.Presentation)
Dim ppSlide As PowerPoint.Slide
Set ppSlide = ppPrez.Slides.Add(sldNum + 1, ppLayoutBlank)
ppPrez.PageSetup.SlideSize = ppSlideSizeLetterPaper
ppSlide.Select
ThisWorkbook.Sheets("roll").Activate
ActiveSheet.ChartObjects("35").Activate
ActiveChart.ChartArea.Copy
ppSlide.Shapes.Paste.Select
With ppSlide.Shapes(1)
.Left = 1 * (ppPrez.PageSetup.SlideWidth / 20)
.Height = _
ppPrez.PageSetup.SlideHeight / 2
.Width = _
9 * (ppPrez.PageSetup.SlideWidth / 10)
.Top = 0
End With
Application.Wait (Now + TimeValue("0:00:03"))
Application.CutCopyMode = False
MsgBox ("done")
ActiveSheet.ChartObjects("40").Activate
ActiveChart.ChartArea.Copy
ppSlide.Shapes.Paste.Select
With ppSlide.Shapes(2)
.Left = 1 * (ppPrez.PageSetup.SlideWidth / 20)
.Height = _
ppPrez.PageSetup.SlideHeight / 2
.Width = _
9 * (ppPrez.PageSetup.SlideWidth / 10)
.Top = _
ppPrez.PageSetup.SlideHeight / 2
End With
Application.Wait (Now + TimeValue("0:00:07"))
MsgBox ("done")
End Sub
sub create_Slide9(sldNum As Long, ppPrez As PowerPoint.Presentation, ppt As PowerPoint.Application)
Dim ppSlide As PowerPoint.Slide
Dim objPres As PowerPoint.Presentation
Set ppSlide = ppPrez.Slides.Add(sldNum + 1, ppLayoutBlank)
ppPrez.PageSetup.SlideSize = ppSlideSizeLetterPaper
ppSlide.Select
myFile = "File Path....same as above"
Set objPres = ppt.Presentations.Open(myFile)
objPres.Slides(8).Copy
ppPrez.Slides.Paste Index:=sldNum + 1 'sometimes it fails here too because of a paste issue (Either vba doesn't like method or no where to paste too)
objPres.Close
ppPrez.Slides(sldNum + 2).Delete
MsgBox ("done")
Application.Wait (Now + TimeValue("0:00:07"))
End Sub

I'm not certain, but I think that message boxes are blocking. Execution is stopped until it's dealt with, so wont give your code time to recover.
The following code should work but I don't really like it. Its the best I can do without modifying some of your other functioning code too.
Hopefully you might see what the idea behind the code is and can improve on it.
Ideally it would use a loop and be inside your CreateNewPresentation sub instead of a recursive function.
You could potentially just replace the messageboxes in your code with Sleep 100 and not use my code (after copying the Sleep Declaration to your module)
PowerPoint doesn't have a ScreenUpdating type deal and some commands do take a while to complete. Using Sleep between each slide may help, it might not. It might be worth putting some Sleep's between some function calls in your create_slideN macros. I've never automated Powerpoint so dont know how it works.
Private Declare Sub Sleep Lib "kernel32" (ByVal lngMilliSeconds As Long)
Public CreationIndex As Integer
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim slideCount As Integer
Sub CreateNewPresentation()
Application.ScreenUpdating = False
Application.EnableEvents = False
If ppApp Is Nothing Then
Set ppApp = New PowerPoint.Application
End If
Set ppPres = ppApp.Presentations.Add
ppPres.SaveAs "FileName"
ppApp.Visible = True
CreationIndex = 1
Create CreationIndex ' start the ball rolling...
End Sub
Sub Create(i As Integer)
slidesCount = ppPres.Slides.Count
Select Case i
Case 1
Call Create_Slide1(slidesCount, ppPres, ppApp)
Case 2
Call create_Slide2(slidesCount, ppPres)
Case 3
Call create_Slide3(slidesCount, ppPres)
Case Else
MsgBox "Complete or Broken...", vbOKOnly
Exit Sub
End Select
Application.CutCopyMode = False
Sleep 200 ' wait for a bit...
CreationIndex = CreationIndex + 1
Create CreationIndex
End Sub

Related

Trim a excel VBA that copies data from various tabs into Powerpoint

I am basically looking for a way to trim below code. Code works just fine. This code takes a range from each tab of excel and paste it into powerpoint and then assigns a title to each slide after pasting, but I feel the code is way too long and can be trimmed. I use excel 2016. Also worth mentioning that everywhere it says repeat, it is basically repeating the copy and paste from excel tab to powerpoint then assigning a title to that slide.
Sub CommercialtoPowerPoint()
'declare variables
Dim otherWB As Workbook
Dim ws As Worksheet
Dim PP As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPslide As PowerPoint.Slide
Dim Sh As PowerPoint.Shape
Dim Bh As PowerPoint.Shape
Dim GSF As Workbook
Dim SlideTitle As String
'opening powerpoint and creating a new presentation
Set GSF = Workbooks("Support Function P&L Details FY23-Update File")
Set PP = New PowerPoint.Application
Set PPPres = PP.Presentations.Add
PP.Visible = True
'adding new slide to PP presentation and using for further use
Set PPslide = PPPres.Slides.Add(1, ppLayoutTitleOnly)
PPslide.Select
'setting slide size from 16:9 to 4:3
PPslide.Application.ActivePresentation.PageSetup.SlideSize = 1 'ppSlideSizeOnScreen = 1
'code to copy range from excel sheet
Sheets("Commercial-H1").Select
Sheets("Commercial-H1").Range("B3:L220").Copy
'pasting picture and adjusting positing
With PPslide.Shapes.PasteSpecial(DataType:=ppPasteEnhancedMetafile)
.Width = 666.72
.Height = 390.24
End With
PP.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
Application.CutCopyMode = False
'Adding title to slide and align center
SlideTitle = "H1 P&L"
PPslide.Shapes.Title.TextFrame.TextRange.Text = SlideTitle
Set Sh = PPslide.Shapes.Title
Sh.Height = 20
Sh.TextEffect.FontBold = msoCTrue
Sh.TextEffect.FontName = Arial
PPslide.Shapes.Title.TextEffect.Alignment = msoTextEffectAlignmentCentered
Application.CutCopyMode = False
'repeat
Set PPslide = PPPres.Slides.Add(1, ppLayoutTitleOnly)
PPslide.Select
Sheets("Commercial-LAM").Select
Sheets("Commercial-LAM").Range("B3:L220").Copy
With PPslide.Shapes.PasteSpecial(DataType:=ppPasteEnhancedMetafile)
.Width = 666.72
.Height = 390.24
End With
PP.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
Application.CutCopyMode = False
SlideTitle = "LAM P&L"
PPslide.Shapes.Title.TextFrame.TextRange.Text = SlideTitle
Set Sh = PPslide.Shapes.Title
Sh.Height = 20
Sh.TextEffect.FontBold = msoCTrue
Sh.TextEffect.FontName = Arial
PPslide.Shapes.Title.TextEffect.Alignment = msoTextEffectAlignmentCentered
Application.CutCopyMode = False
'repeat
Set PPslide = PPPres.Slides.Add(1, ppLayoutTitleOnly)
PPslide.Select
Sheets("Commercial-EMEA").Select
Sheets("Commercial-EMEA").Range("B3:L220").Copy
With PPslide.Shapes.PasteSpecial(DataType:=ppPasteEnhancedMetafile)
.Width = 666.72
.Height = 390.24
End With
PP.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
Application.CutCopyMode = False
SlideTitle = "EMEA P&L"
PPslide.Shapes.Title.TextFrame.TextRange.Text = SlideTitle
Set Sh = PPslide.Shapes.Title
Sh.Height = 20
Sh.TextEffect.FontBold = msoCTrue
Sh.TextEffect.FontName = Arial
PPslide.Shapes.Title.TextEffect.Alignment = msoTextEffectAlignmentCentered
Application.CutCopyMode = False
'repeat
Set PPslide = PPPres.Slides.Add(1, ppLayoutTitleOnly)
PPslide.Select
Sheets("Commercial-APAC").Select
Sheets("Commercial-APAC").Range("B3:L220").Copy
With PPslide.Shapes.PasteSpecial(DataType:=ppPasteEnhancedMetafile)
.Width = 666.72
.Height = 390.24
End With
PP.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
Application.CutCopyMode = False
SlideTitle = "APAC P&L"
PPslide.Shapes.Title.TextFrame.TextRange.Text = SlideTitle
Set Sh = PPslide.Shapes.Title
Sh.Height = 20
Sh.TextEffect.FontBold = msoCTrue
Sh.TextEffect.FontName = Arial
PPslide.Shapes.Title.TextEffect.Alignment = msoTextEffectAlignmentCentered
Application.CutCopyMode = False
'repeat
Set PPslide = PPPres.Slides.Add(1, ppLayoutTitleOnly)
PPslide.Select
Sheets("Commercial-HS Admin").Select
Sheets("Commercial-HS Admin").Range("B3:L220").Copy
With PPslide.Shapes.PasteSpecial(DataType:=ppPasteEnhancedMetafile)
.Width = 666.72
.Height = 390.24
End With
PP.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
Application.CutCopyMode = False
SlideTitle = "HS Admin P&L"
PPslide.Shapes.Title.TextFrame.TextRange.Text = SlideTitle
Set Sh = PPslide.Shapes.Title
Sh.Height = 20
Sh.TextEffect.FontBold = msoCTrue
Sh.TextEffect.FontName = Arial
PPslide.Shapes.Title.TextEffect.Alignment = msoTextEffectAlignmentCentered
Application.CutCopyMode = False
'repeat
Set PPslide = PPPres.Slides.Add(1, ppLayoutTitleOnly)
PPslide.Select
Sheets("Commercial-Corp").Select
Sheets("Commercial-Corp").Range("B3:L220").Copy
With PPslide.Shapes.PasteSpecial(DataType:=ppPasteEnhancedMetafile)
.Width = 666.72
.Height = 390.24
End With
PP.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
Application.CutCopyMode = False
SlideTitle = "Corp P&L"
PPslide.Shapes.Title.TextFrame.TextRange.Text = SlideTitle
Set Sh = PPslide.Shapes.Title
Sh.Height = 20
Sh.TextEffect.FontBold = msoCTrue
Sh.TextEffect.FontName = Arial
PPslide.Shapes.Title.TextEffect.Alignment = msoTextEffectAlignmentCentered
Application.CutCopyMode = False
'repeat
Set PPslide = PPPres.Slides.Add(1, ppLayoutTitleOnly)
PPslide.Select
Sheets("Commercial-all").Select
Sheets("Commercial-all").Range("B3:L220").Copy
With PPslide.Shapes.PasteSpecial(DataType:=ppPasteEnhancedMetafile)
.Width = 666.72
.Height = 390.24
End With
PP.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
Application.CutCopyMode = False
SlideTitle = "Full P&L"
PPslide.Shapes.Title.TextFrame.TextRange.Text = SlideTitle
Set Sh = PPslide.Shapes.Title
Sh.Height = 20
Sh.TextEffect.FontBold = msoCTrue
Sh.TextEffect.FontName = Arial
PPslide.Shapes.Title.TextEffect.Alignment = msoTextEffectAlignmentCentered
Application.CutCopyMode = False
'Adding slide for Headcount and moving to last slide
Dim slideCount As Long
slideCount = PPPres.Slides.Count
Set PPslide = PPPres.Slides.Add(slideCount + 1, ppLayoutTitleOnly)
PPslide.Select
PPslide.Shapes(1).TextFrame.TextRange.Text = "Headcount"
Set Sh = PPslide.Shapes.Title
Sh.Height = 20
Sh.TextEffect.FontBold = msoCTrue
Sh.TextEffect.FontName = Arial
PPslide.Shapes.Title.TextEffect.Alignment = msoTextEffectAlignmentCentered
'setting powerpoint title
Set PPslide = PPPres.Slides.Add(1, ppLayoutTitle)
PPslide.Select
PPslide.Shapes(1).TextFrame.TextRange.Text = "Monthly P&L Report"
PPslide.Shapes(2).TextFrame.TextRange.Text = "Commercial"
'back to excel sheet and select cell A1 in every sheet
GSF.Activate
Application.CutCopyMode = False
For Each ws In GSF.Sheets
ws.Activate
ws.[a1].Select
Next ws
GSF.Worksheets(1).Activate
'powerpoint memory cleanup
PP.Activate
Set PPslide = Nothing
Set PPPres = Nothing
Set PP = Nothing
Set Sh = Nothing
Set Bh = Nothing
Set GSF = Nothing
End Sub
I took some bits and pieces and trimmed them, but I feel there is room for more.
This new version of the code uses two arrays, one for the sheet names and another one for the slide titles. It also uses a loop to iterate through the sheets and titles. This way, you don't need to repeat the same code multiple times.
Also removed the unused variables and made the font name a string.
Sub CommercialtoPowerPoint()
'declare variables
Dim PP As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPslide As PowerPoint.Slide
Dim Sh As PowerPoint.Shape
Dim slideTitle As String
'opening powerpoint and creating a new presentation
Set PP = New PowerPoint.Application
Set PPPres = PP.Presentations.Add
PP.Visible = True
'setting slide size from 16:9 to 4:3
PPPres.PageSetup.SlideSize = 1
'Array of sheet names
Dim sheetNames() As String
sheetNames = Array("Commercial-H1", "Commercial-LAM", "Commercial-EMEA")
'Array of slide titles
Dim slideTitles() As String
slideTitles = Array("H1 P&L", "LAM P&L", "EMEA P&L")
'loop through the sheets
For i = 0 To UBound(sheetNames)
'adding new slide to PP presentation and using for further use
Set PPslide = PPPres.Slides.Add(1, ppLayoutTitleOnly)
PPslide.Select
'code to copy range from excel sheet
Sheets(sheetNames(i)).Range("B3:L220").Copy
'pasting picture and adjusting positing
With PPslide.Shapes.PasteSpecial(DataType:=ppPasteEnhancedMetafile)
.Width = 666.72
.Height = 390.24
End With
PP.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
'Adding title to slide and align center
slideTitle = slideTitles(i)
PPslide.Shapes.Title.TextFrame.TextRange.Text = slideTitle
Set Sh = PPslide.Shapes.Title
Sh.Height = 20
Sh.TextEffect.FontBold = msoCTrue
Sh.TextEffect.FontName = "Arial"
PPslide.Shapes.Title.TextEffect.Alignment = msoTextEffectAlignmentCentered
Next i
Application.CutCopyMode = False
End Sub
I figured it out. #German code is good, but needs 2 changes below:
Declare (this was missing)
Dim i As Integer
and change 2 lines below
Dim sheetNames() As String
This needs to be
Dim sheetNames() As Variant
and
Dim slideTitles() As String
needs to be
Dim slideTitles() As Variant
Minor correction and this solved the problem!

VBA: Exporting charts from Excel to PowerPoint, but my PowerPoint Application crashes without an error message

My goal: From Microsoft Excel, launch a PowerPoint Presentation ("PP") template. Once the PP file is visible, I am copying the first chart from Microsoft Excel, then adding the first chart to the first PowerPoint slide. The first chart and first slide are working as expected. However, I am taking the second chart from Microsoft Excel and am not able to paste it in the second PP slide because my whole PP application is crashing without an error message. After debugging, I have found that line 53 in the code is causing the error, but I am unsure as to why or how to resolve.
Beginning of the Screenshot:
Ending of the Screenshot and the Beginning of the VBA syntax:
Sub TJT()
Dim PPT_App As PowerPoint.Application
Dim PPT_Pres As PowerPoint.Presentation
Dim PPT_Slide As PowerPoint.Slide
Dim PPT_Shape As PowerPoint.Shape
Dim wk_Book As Workbook
Set wk_Book = ThisWorkbook
Dim slideCount As Integer
slideCount = 1
''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''SLIDE 1 BEGINNING'''''''''''
''''''''''''''''''''''''''''''''''''''''''''
TemplateFile = "C:\Users\john-doe\Desktop\Default_Template_v0.potx"
Application.ScreenUpdating = False
Set PPT_App = CreateObject("PowerPoint.Application")
Set PPT_Pres = PPT_App.Presentations.Open(TemplateFile, msoFalse, msoTrue)
Set PPT_Slide = PPT_Pres.Slides(slideCount)
PPT_App.Visible = True
Application.Wait (Now + TimeValue("0:00:03"))
Set Chart1 = Worksheets("Inputs").ChartObjects("XY_Return_Vol")
Chart1.Copy
PPT_Slide.Shapes.Paste.Select
With PPT_App.ActiveWindow.Selection.ShapeRange(1)
.LockAspectRatio = False
.Height = 5.2 * 72
.Width = 5.5 * 72
.Left = 1 * 72
.Top = 1.72 * 72
End With
slideCount = slideCount + 1
Set PPT_Shape = Nothing
Set PPT_Slide = Nothing
wk_Book.Application.CutCopyMode = False
PPT_App.ActiveWindow.Selection.Unselect
''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''SLIDE 2 BEGINNING'''''''''''
''''''''''''''''''''''''''''''''''''''''''''
Set PPT_Slide = PPT_Pres.Slides(slideCount)
PPT_Pres.Slides(slideCount).Select
PPT_App.ActiveWindow.Selection.Unselect
wk_Book.Application.CutCopyMode = True
Set Chart2 = Worksheets("Inputs").ChartObjects("Ann_Ret")
Chart2.Copy
PPT_Slide.Shapes.Paste.Select 'This is line in the code that causes the PowerPoint to crash
With PPT_App.ActiveWindow.Selection.ShapeRange(1)
.LockAspectRatio = False
.Height = 5.2 * 72
.Width = 11.33 * 72
.Left = 1 * 72
.Top = 1.72 * 72
End With
End Sub

Increase the width and height of the image extracted

I am extracting a excel table as png file the extraction is ok, but the extracted file is too zoomed out and when i zoom in it is all blurr, i am going to attach the image in a mail so the data should be clear visible,Any idea where to change the code ,changed the width and height below to almost 50 no change
With ChTemp.Parent
.Width = PicTemp.Width + 8
.Height = PicTemp.Height + 8
Here is the code
Sub check(filename As String, picturename As String)
Dim myWb As Workbook
Dim ws As Worksheet
Dim i, j As Long, LastRow As Integer
'Dim filename As String, picturename As String
Set myWb = Workbooks.Open(filename:=filename)
Worksheets("Sheet1").Activate
Dim FName As String
FName = picturename
Dim pic_rng As Range
Dim ShTemp As Worksheet
Dim ChTemp As Chart
Dim PicTemp As Picture
Application.ScreenUpdating = False
Set pic_rng = ActiveSheet.Range("A2:N42")
Set ShTemp = Worksheets.Add
Charts.Add
ActiveChart.Location Where:=xlLocationAsObject, Name:=ShTemp.Name
Set ChTemp = ActiveChart
pic_rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
ChTemp.Paste
Set PicTemp = Selection
With ChTemp.Parent
.Width = PicTemp.Width + 8
.Height = PicTemp.Height + 8
End With
ChTemp.Export filename:=picturename, FilterName:="jpg"
'UserForm1.Image1.Picture = LoadPicture(FName)
Kill FName
Application.DisplayAlerts = False
ShTemp.Delete
myWb.Close False
End Sub
The selection in your case is the Chart itself, and nothing will be increase n its dimensions. And the code must paste the picture in the increased chart... Otherwise, any increase does not help.
Try the next way, please:
'what you have in your code...
'.....
pic_rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
With ChTemp.Parent
.Width = rng.Width + 10
.Height = rng.Height + 10
End With
ChTemp.Paste
ChTemp.Export filename:=picturename, FilterName:="jpg"
'your code...
'...

Excel to PowerPoint

I am trying to prepare a presentation from Excel. As of now VBA code is preparing "n number of "presentations as per no of times Loop runs. I want Code to generate just 1 presentation with all slides combined. Fist Macro "Addnumber" is run, which run Macro "ExcelRangeToPowerPoint". Its Macro "ExcelRangeToPowerPoint"which need to add slides for every loop of Macro "Addnumber"
Please Support
Sub AddNumber()
Dim Ws As Worksheet
Dim rngSel As Range
Dim rng As Range
Dim Num As Double
Dim i As Long
Dim j As Long
Dim lAreas As Long
Dim lRows As Long
Dim lCols As Long
Dim Arr() As Variant
Set rngSel = Worksheets("Sheet1").Range("A5:A30")
Do Until Range("A30") = Range("A3")
Num = 26
For Each rng In rngSel.Areas
If rng.Count = 1 Then
rng = rng + Num
Else
lRows = rng.Rows.Count
lCols = rng.Columns.Count
Arr = rng
For i = 1 To lRows
For j = 1 To lCols
Arr(i, j) = Arr(i, j) + Num
Next j
Next i
rng.Value = Arr
End If
Call ExcelRangeToPowerPoint
Next rng
Loop
End Sub
Sub ExcelRangeToPowerPoint()
'PURPOSE: Copy/Paste An Excel Range Into a New PowerPoint Presentation
Dim rng As Range
Dim rng2 As Range
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object
Dim mySize As PageSetup
Dim Addtitle As Shape
Dim DateT As String
'Copy Range from Excel
Set rng = Worksheets("Sheet1").Range("E2:M30")
Set rng2 = Worksheets("Sheet1").Range("F2")
Set rng3 = Worksheets("Sheet1").Range("B3")
'Create an Instance of PowerPoint
On Error Resume Next
'Is PowerPoint already opened?
Set PowerPointApp = GetObject(class:="PowerPoint.Application")
'Clear the error between errors
Err.Clear
'If PowerPoint is not already open then open PowerPoint
If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
'Handle if the PowerPoint Application is not found
If Err.Number = 429 Then
MsgBox "PowerPoint could not be found, aborting."
Exit Sub
End If
On Error GoTo 0
'Optimize Code
Application.ScreenUpdating = False
'Create a New Presentation
Set myPresentation = PowerPointApp.Presentations.Add
'Add a slide to the Presentation
Set mySlide = myPresentation.Slides.Add(1, 11)
'11 = ppLayoutTitleOnly
'Change Theme and Layout
mySlide.ApplyTheme "C:\Users\davinder.sond\AppData\Roaming\Microsoft\Templates\Document Themes\DefaultTheme.thmx"
myPresentation.PageSetup.SlideSize = 3
myPresentation.Slides(1).Shapes.Title.TextFrame.TextRange.Text = rng2
myPresentation.Slides(1).Shapes.Title.Left = 59
myPresentation.Slides(1).Shapes.Title.Top = 10
myPresentation.Slides(1).Shapes.Title.Height = 30
myPresentation.Slides(1).Shapes.Title.Width = 673
With myPresentation.Slides(1).Shapes.Title
With .TextFrame.TextRange.Font
.Size = 24
.Name = "Arial"
.Bold = True
.Color.RGB = RGB(255, 255, 255)
End With
End With
'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.LockAspectRatio = 0
myShape.Left = 12
myShape.Top = 55
myShape.Height = 475
myShape.Width = 756
'Make PowerPoint Visible and Active
PowerPointApp.Visible = True
PowerPointApp.Activate
DateT = Format("h:mm:ss")
'Clear The Clipboard
Application.CutCopyMode = False
myPresentation.SaveAs "C:\Project Control CCJV\ExperimentsPunch\" & rng3 & ".pptm"
PowerPointApp.Quit
End Sub
You are creating a new presentation everytime you call Set myPresentation = PowerPointApp.Presentations.Add within ExcelRangeToPowerPoint().
You can either try to open/close the Presentation outside of ExcelRangeToPowerPoint() and add a parameter to the function like ExcelRangeToPowerPoint(myPresentationObject) then you can simple add the slides there
or
you call the function AddNumber() within ExcelRangeToPowerPoint() of coarse the you need to loop there...
e.g.
Sub ExcelRangeToPowerPoint()
' some preparative code
'Create a New Presentation
Set myPresentation = PowerPointApp.Presentations.Add
'Add a slide to the Presentation
Set mySlide = myPresentation.Slides.Add(1, 11)
For Each rng in rngSel.Areas
'Filling the presentation one slide at a time
AddSlide(rng)
Next
'Clear The Clipboard
Application.CutCopyMode = False
myPresentation.SaveAs "C:\Project Control CCJV\ExperimentsPunch\" & rng3 &
".pptm"
PowerPointApp.Quit
'some more code
End Sub

Paste Excel Chart into Powerpoint using VBA

I'm trying to create an excel macro that copies charts displayed on an excel sheet, and pastes them (paste special) into a PowerPoint. The problem I'm having is how do I paste each chart on a different slide? I do not know the syntax at all..
This is what I have so far (it works but it only pastes to the first sheet):
Sub graphics3()
Sheets("Chart1").Select
ActiveSheet.ChartObjects("Chart1").Activate
ActiveChart.ChartArea.Copy
Sheets("Graphs").Select
range("A1").Select
ActiveSheet.Paste
With ActiveChart.Parent
.Height = 425 ' resize
.Width = 645 ' resize
.Top = 1 ' reposition
.Left = 1 ' reposition
End With
Dim PPT As Object
Set PPT = CreateObject("PowerPoint.Application")
PPT.Visible = True
PPT.Presentations.Open Filename:="locationwherepptxis"
Set PPApp = GetObject("Powerpoint.Application")
Set PPPres = PPApp.activepresentation
Set PPSlide = PPPres.slides _
(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex)
' Copy chart as a picture
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, _
Format:=xlPicture
' Paste chart
PPSlide.Shapes.Paste.Select
' Align pasted chart
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
Given I dont have your file locations to work with I have attached a routine below that
Created a new instance of PowerPoint (late binding, hence the need to define constants for ppViewSlide etc)
Loops through each chart in a sheet called Chart1 (as per your example)
Adds a new slide
Pastes each chart, then repeats
Did you need to format each chart picture before exporting for size, or can you change your default chart size?
Const ppLayoutBlank = 2
Const ppViewSlide = 1
Sub ExportChartstoPowerPoint()
Dim PPApp As Object
Dim chr
Set PPApp = CreateObject("PowerPoint.Application")
PPApp.Presentations.Add
PPApp.ActiveWindow.ViewType = ppViewSlide
For Each chr In Sheets("Chart1").ChartObjects
PPApp.ActivePresentation.Slides.Add PPApp.ActivePresentation.Slides.Count + 1, ppLayoutBlank
PPApp.ActiveWindow.View.GotoSlide PPApp.ActivePresentation.Slides.Count
chr.Select
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
PPApp.ActiveWindow.View.Paste
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
Next chr
PPApp.Visible = True
End Sub
Code with function for plotting 6 charts from Excel to PPT
Option Base 1
Public ppApp As PowerPoint.Application
Sub CopyChart()
Dim wb As Workbook, ws As Worksheet
Dim oPPTPres As PowerPoint.Presentation
Dim myPPT As String
myPPT = "C:\LearnPPT\MyPresentation2.pptx"
Set ppApp = CreateObject("PowerPoint.Application")
'Set oPPTPres = ppApp.Presentations("MyPresentation2.pptx")
Set oPPTPres = ppApp.Presentations.Open(Filename:=myPPT)
ppApp.Visible = True
Set wb = ThisWorkbook
Set ws = wb.Sheets(1)
i = 1
For Each shp In ws.Shapes
strShapename = "C" & i
ws.Shapes(shp.Name).Name = strShapename
'shpArray.Add (shp)
i = i + 1
Next shp
Call Plot6Chart(oPPTPres, 2, ws.Shapes(1), ws.Shapes(2), ws.Shapes(3), ws.Shapes(4), ws.Shapes(5), ws.Shapes(6))
End Sub
Function Plot6Chart(pPres As Presentation, SlideNo As Long, ParamArray cCharts())
Dim oSh As Shape
Dim pSlide As Slide
Dim lLeft As Long, lTop As Long
Application.CutCopyMode = False
Set pSlide = pPres.Slides(SlideNo)
For i = 0 To UBound(cCharts)
cCharts(i).Copy
ppApp.ActiveWindow.View.GotoSlide SlideNo
pSlide.Shapes.Paste
Application.CutCopyMode = False
If i = 0 Then ' 1st Chart
lTop = 0
lLeft = 0
ElseIf i = 1 Then ' 2ndChart
lLeft = lLeft + 240
ElseIf i = 2 Then ' 3rd Chart
lLeft = lLeft + 240
ElseIf i = 3 Then ' 4th Chart
lTop = lTop + 270
lLeft = 0
ElseIf i = 4 Then ' 5th Chart
lLeft = lLeft + 240
ElseIf i = 5 Then ' 6th Chart
lLeft = lLeft + 240
End If
pSlide.Shapes(cCharts(i).Name).Left = lLeft
pSlide.Shapes(cCharts(i).Name).Top = lTop
Next i
Set oSh = Nothing
Set pSlide = Nothing
Set oPPTPres = Nothing
Set ppApp = Nothing
Set pPres = Nothing
End Function

Resources