Results 1 to 2 of 2

Thread: Excel to Powerpoint VBA Code

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Excel to Powerpoint VBA Code

    Hello,

    I have code below that transfers charts from Excel to Powerpoint perfectly. My question what code do I need to insert to transfer numerical values?

    Code:
    Option Explicit
    
    
    '------------------
    ' optionnal
    ' To add reference to PowerPoint Library when tools > references not available.
    '
    Sub AddReReference()
       ThisWorkbook.VBProject.References.AddFromFile _
             "C:\Program Files (x86)\Microsoft Office\Office14\MSppt.OLB"
    End Sub
    '------------------
    
    '---------------------------------------------------------------------------------------
    ' Procedure : GetFileName
    ' Author
    ' Date : 7/29/2013
    ' Purpose :
    '---------------------------------------------------------------------------------------
    '
    Function GetFileName() As String
    
       Dim strNewFN As String
    
       On Error GoTo GetFileName_Error
       'FileFilter:="Excel Files (*.xls), *.xls", Title:="Please select a file"
       strNewFN = Application.GetOpenFilename()
       GetFileName = strNewFN
       On Error GoTo 0
       Exit Function
    GetFileName_Error:
       MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure GetFileName of Module Module3 On Line " & Erl
    End Function
    
    ' Code to export data, charts, etc.
    '---------------------------------------------------------------------------------------
    ' Procedure : PopulatePowerPoint
    ' Author :
    ' Date : 7/29/2013
    ' Purpose :
    '---------------------------------------------------------------------------------------
    '
    Sub PopulatePowerPoint()
    'Add a reference to the Microsoft PowerPoint Library by:
    '1. Go to Tools in the VBA menu
    '2. Click on Reference
    '3. Scroll down to Microsoft PowerPoint X.0 Object Library, check the box, and press Okay
    'First we declare the variables we will be using
    'Dim newPowerPoint As PowerPoint.Application
    
    
       Dim ppSlide As PowerPoint.Slide
       Dim strPlaceHolder As String
       Dim strPowerPointFileName As String
       Dim wsWorksheets As Excel.Worksheet
       Dim choCharts As Excel.ChartObject
       Dim ppApp As PowerPoint.Application
       Dim PPPres As PowerPoint.Presentation
       Dim cht As Excel.ChartObject
    
    
       On Error GoTo PopulatePowerPoint_Error
       'Get PPT file name
       strPowerPointFileName = GetFileName
    
       'Open PPT
       Set ppApp = CreateObject("PowerPoint.Application")
       ppApp.Visible = msoTrue
       Set PPPres = ppApp.Presentations.Open(strPowerPointFileName)
       ppApp.ActiveWindow.ViewType = ppViewSlide
    
       'Call function to add selected chart in the PPT
       ' Arguments are: PPTFile, ChartObject, Top, Left, Width, Height
       ' Ajust position and dimensions as required.
       'Chart no1
       Set cht = ThisWorkbook.Sheets("Dept Lines").ChartObjects("Chart 1")
       Call ChartsToPPT(PPPres, 3, cht, 100, 100, 100, 300)
    
       'Chart no2
       Set cht = ThisWorkbook.Sheets("Dept Lines").ChartObjects("Chart 2")
       Call ChartsToPPT(PPPres, 4, cht, 100, 150, 200, 350)
    
       'Chart no3
       Set cht = ThisWorkbook.Sheets("Qty Per Line Shipped").ChartObjects("Chart 1")
       Call ChartsToPPT(PPPres, 5, cht, 125, 100, 300, 300)
    
       'Chart no4
       Set cht = ThisWorkbook.Sheets("UM Analysis").ChartObjects("Chart 1")
       Call ChartsToPPT(PPPres, 6, cht, 150, 400, 300, 300)
    
       'Chart no5
       Set cht = ThisWorkbook.Sheets("Qty Per Line Shipped").ChartObjects("Chart 2")
       Call ChartsToPPT(PPPres, 6, cht, 150, 10, 300, 300)
    
       'Chart no6
       Set cht = ThisWorkbook.Sheets("UM Analysis").ChartObjects("Chart 2")
       Call ChartsToPPT(PPPres, 7, cht, 150, 350, 300, 300)
    
       'Chart no7
       Set cht = ThisWorkbook.Sheets("Qty Per Line Shipped").ChartObjects("Chart 3")
       Call ChartsToPPT(PPPres, 7, cht, 150, 10, 300, 300)
    
       'Chart no8
       Set cht = ThisWorkbook.Sheets("UM Analysis").ChartObjects("Chart 3")
       Call ChartsToPPT(PPPres, 8, cht, 150, 350, 300, 300)
    
       'Chart no9
       Set cht = ThisWorkbook.Sheets("Qty Per Line Shipped").ChartObjects("Chart 4")
       Call ChartsToPPT(PPPres, 8, cht, 150, 10, 300, 300)
    
    
       On Error GoTo 0
    CleanUp:
       Set cht = Nothing
       Set PPPres = Nothing
       Set ppSlide = Nothing
       Set ppApp = Nothing
    
       Exit Sub
    PopulatePowerPoint_Error:
       MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure PopulatePowerPoint of Module Module3 On Line " & Erl
       GoTo CleanUp
    
    End Sub
    
    
    ' Code to copy selected chart to specified slide.
    Sub ChartsToPPT(oPPT As PowerPoint.Presentation, iSlideNo As Integer, _
                    cht As ChartObject, iTop As Integer, iLeft As Integer, iWidth As Integer, iHeight As Integer)
    
       Dim ppSlide As PowerPoint.Slide
       Dim pSh As PowerPoint.Shape
    
       'Select slide
       Set ppSlide = oPPT.Slides(iSlideNo)
    
       'cht.Copy
       cht.Chart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
       With ppSlide
          .Shapes.Paste
          '.Shapes.PasteSpecial DataType:=ppPasteMetafilePicture
          Set pSh = .Shapes(.Shapes.Count)  '.Select  'Select the last shape
       End With
       With pSh
          .Top = iTop
          .Left = iLeft
          .Width = iWidth
          .Height = iHeight
       End With
    End Sub
    Last edited by Admin; 08-30-2013 at 08:54 AM.

  2. #2
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0
    Code:
    Option Explicit
    
    
    '------------------
    ' optionnal
    ' To add reference to PowerPoint Library when tools > references not available.
    '
    Sub AddReReference()
       ThisWorkbook.VBProject.References.AddFromFile _
             "C:\Program Files (x86)\Microsoft Office\Office14\MSppt.OLB"
    End Sub
    '------------------
    
    '---------------------------------------------------------------------------------------
    ' Procedure : GetFileName
    ' Author
    ' Date : 7/29/2013
    ' Purpose :
    '---------------------------------------------------------------------------------------
    '
    Function GetFileName() As String
    
       Dim strNewFN As String
    
       On Error GoTo GetFileName_Error
       'FileFilter:="Excel Files (*.xls), *.xls", Title:="Please select a file"
       strNewFN = Application.GetOpenFilename()
       GetFileName = strNewFN
       On Error GoTo 0
       Exit Function
    GetFileName_Error:
       MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure GetFileName of Module Module3 On Line " & Erl
    End Function
    
    ' Code to export data, charts, etc.
    '---------------------------------------------------------------------------------------
    ' Procedure : PopulatePowerPoint
    ' Author :
    ' Date : 7/29/2013
    ' Purpose :
    '---------------------------------------------------------------------------------------
    '
    Sub PopulatePowerPoint()
    'Add a reference to the Microsoft PowerPoint Library by:
    '1. Go to Tools in the VBA menu
    '2. Click on Reference
    '3. Scroll down to Microsoft PowerPoint X.0 Object Library, check the box, and press Okay
    'First we declare the variables we will be using
    'Dim newPowerPoint As PowerPoint.Application
    
    
       Dim ppSlide As PowerPoint.Slide
       Dim strPlaceHolder As String
       Dim strPowerPointFileName As String
       Dim wsWorksheets As Excel.Worksheet
       Dim choCharts As Excel.ChartObject
       Dim ppApp As PowerPoint.Application
       Dim PPPres As PowerPoint.Presentation
       Dim cht As Excel.ChartObject
    
    
       On Error GoTo PopulatePowerPoint_Error
       'Get PPT file name
       strPowerPointFileName = GetFileName
    
       'Open PPT
       Set ppApp = CreateObject("PowerPoint.Application")
       ppApp.Visible = msoTrue
       Set PPPres = ppApp.Presentations.Open(strPowerPointFileName)
       ppApp.ActiveWindow.ViewType = ppViewSlide
    
       'Call function to add selected chart in the PPT
       ' Arguments are: PPTFile, ChartObject, Top, Left, Width, Height
       ' Ajust position and dimensions as required.
       'Chart no1
       Set cht = ThisWorkbook.Sheets("Dept Lines").ChartObjects("Chart 1")
       Call ChartsToPPT(PPPres, 3, cht, 100, 100, 100, 300)
    
       'Chart no2
       Set cht = ThisWorkbook.Sheets("Dept Lines").ChartObjects("Chart 2")
       Call ChartsToPPT(PPPres, 4, cht, 100, 150, 200, 350)
    
       'Chart no3
       Set cht = ThisWorkbook.Sheets("Qty Per Line Shipped").ChartObjects("Chart 1")
       Call ChartsToPPT(PPPres, 5, cht, 125, 100, 300, 300)
    
       'Chart no4
       Set cht = ThisWorkbook.Sheets("UM Analysis").ChartObjects("Chart 1")
       Call ChartsToPPT(PPPres, 6, cht, 150, 400, 300, 300)
    
       'Chart no5
       Set cht = ThisWorkbook.Sheets("Qty Per Line Shipped").ChartObjects("Chart 2")
       Call ChartsToPPT(PPPres, 6, cht, 150, 10, 300, 300)
    
       'Chart no6
       Set cht = ThisWorkbook.Sheets("UM Analysis").ChartObjects("Chart 2")
       Call ChartsToPPT(PPPres, 7, cht, 150, 350, 300, 300)
    
       'Chart no7
       Set cht = ThisWorkbook.Sheets("Qty Per Line Shipped").ChartObjects("Chart 3")
       Call ChartsToPPT(PPPres, 7, cht, 150, 10, 300, 300)
    
       'Chart no8
       Set cht = ThisWorkbook.Sheets("UM Analysis").ChartObjects("Chart 3")
       Call ChartsToPPT(PPPres, 8, cht, 150, 350, 300, 300)
    
       'Chart no9
       Set cht = ThisWorkbook.Sheets("Qty Per Line Shipped").ChartObjects("Chart 4")
       Call ChartsToPPT(PPPres, 8, cht, 150, 10, 300, 300)
    
    
       On Error GoTo 0
    CleanUp:
       Set cht = Nothing
       Set PPPres = Nothing
       Set ppSlide = Nothing
       Set ppApp = Nothing
    
       Exit Sub
    PopulatePowerPoint_Error:
       MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure PopulatePowerPoint of Module Module3 On Line " & Erl
       GoTo CleanUp
    
    End Sub
    
    
    ' Code to copy selected chart to specified slide.
    Sub ChartsToPPT(oPPT As PowerPoint.Presentation, iSlideNo As Integer, _
                    cht As ChartObject, iTop As Integer, iLeft As Integer, iWidth As Integer, iHeight As Integer)
    
       Dim ppSlide As PowerPoint.Slide
       Dim pSh As PowerPoint.Shape
    
       'Select slide
       Set ppSlide = oPPT.Slides(iSlideNo)
    
       'cht.Copy
       cht.Chart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
       With ppSlide
          .Shapes.Paste
          '.Shapes.PasteSpecial DataType:=ppPasteMetafilePicture
          Set pSh = .Shapes(.Shapes.Count)  '.Select  'Select the last shape
       End With
       With pSh
          .Top = iTop
          .Left = iLeft
          .Width = iWidth
          .Height = iHeight
       End With
    End Sub
    Last edited by Admin; 08-30-2013 at 08:55 AM.

Similar Threads

  1. Copy/Paste Excel Range/Chart into Powerpoint VBA
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 1
    Last Post: 03-13-2014, 02:59 PM
  2. Replies: 1
    Last Post: 07-19-2013, 08:23 PM
  3. Embed an Excel Chart into PowerPoint - VBA
    By Junoon in forum Excel Help
    Replies: 3
    Last Post: 07-18-2013, 12:20 AM
  4. Excel VBA Code to Add New Sheets
    By cdurfey in forum Excel Help
    Replies: 1
    Last Post: 06-25-2013, 08:05 AM
  5. Add VBA Reference From Another Application Excel To PowerPoint
    By ds1001 in forum Rajan Verma's Corner
    Replies: 1
    Last Post: 06-02-2013, 02:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •