EXcel VBA : Excel Macro to create table in a PowerPoint
Asked Answered
I

3

7

My requirement is I have a Excel which contains some data. I would like to select some data from the excel and open a PowerPoint file and

Create Table in PowerPoint and populate the data in to it

Right now I have succeeded in collecting the data from excel opening a PowerPoint file through Excel VBA Code.

Code for Opening the PowerPoint from Excel.

    Set objPPT = CreateObject("Powerpoint.application")
    objPPT.Visible = True
    Dim file As String
    file = "C:\Heavyhitters_new.ppt"
    Set pptApp = CreateObject("PowerPoint.Application")
    Set pptPres = pptApp.Presentations.Open(file)

Now how do I create the table in PowerPoint from Excel and populate the data.

Timely help will be very much appreciated.

Thanks in advance,

Isoclinal answered 6/8, 2010 at 14:25 Comment(0)
D
5

Here's some code from http://mahipalreddy.com/vba.htm

''# Code by Mahipal Padigela
''# Open Microsoft Powerpoint,Choose/Insert a Table type Slide(No.4), then double click to add a...
''# ...Table(3 Cols & 2 Rows) then rename the Table to "Table1", Save and Close the Presentation
''# Open Microsoft Excel, add some test data to Sheet1(This example assumes that you have some data in...
''# ... Rows 1,2 and Columns 1,2,3)
''# Open VBA editor(Alt+F11),Insert a Module and Paste the following code in to the code window
''# Reference 'Microsoft Powerpoint Object Library' (VBA IDE-->tools-->references)
''# Change "strPresPath" with full path of the Powerpoint Presentation created earlier.
''# Change "strNewPresPath" to where you want to save the new Presnetation to be created later
''# Close VB Editor and run this Macro from Excel window(Alt+F8) 

Dim oPPTApp As PowerPoint.Application
Dim oPPTShape As PowerPoint.Shape
Dim oPPTFile As PowerPoint.Presentation
Dim SlideNum As Integer
Sub PPTableMacro()
    Dim strPresPath As String, strExcelFilePath As String, strNewPresPath As String
    strPresPath = "H:\PowerPoint\Presentation1.ppt"
    strNewPresPath = "H:\PowerPoint\new1.ppt"

    Set oPPTApp = CreateObject("PowerPoint.Application")
    oPPTApp.Visible = msoTrue
    Set oPPTFile = oPPTApp.Presentations.Open(strPresPath)
    SlideNum = 1
    oPPTFile.Slides(SlideNum).Select
    Set oPPTShape = oPPTFile.Slides(SlideNum).Shapes("Table1")

    Sheets("Sheet1").Activate
    oPPTShape.Table.Cell(1, 1).Shape.TextFrame.TextRange.Text = Cells(1, 1).Text
    oPPTShape.Table.Cell(1, 2).Shape.TextFrame.TextRange.Text = Cells(1, 2).Text
    oPPTShape.Table.Cell(1, 3).Shape.TextFrame.TextRange.Text = Cells(1, 3).Text
    oPPTShape.Table.Cell(2, 1).Shape.TextFrame.TextRange.Text = Cells(2, 1).Text
    oPPTShape.Table.Cell(2, 2).Shape.TextFrame.TextRange.Text = Cells(2, 2).Text
    oPPTShape.Table.Cell(2, 3).Shape.TextFrame.TextRange.Text = Cells(2, 3).Text

    oPPTFile.SaveAs strNewPresPath
    oPPTFile.Close
    oPPTApp.Quit

    Set oPPTShape = Nothing
    Set oPPTFile = Nothing
    Set oPPTApp = Nothing

    MsgBox "Presentation Created", vbOKOnly + vbInformation
End Sub
Deane answered 6/8, 2010 at 14:56 Comment(1)
This was useful too some extend... I have completed it.. thanks a lot for your help.Isoclinal
J
5

This Excel-VBA exports the selected range from Excel to a PowerPoint native table. It also works with merged cells.

Sub Export_Range()

    Dim pp As New PowerPoint.Application
    Dim ppt As PowerPoint.Presentation
    Dim sld As PowerPoint.Slide
    Dim shpTable As PowerPoint.Shape
    Dim i As Long, j As Long

    Dim rng As Excel.Range
    Dim sht As Excel.Worksheet

    Set rng = Selection

    pp.Visible = True
    If pp.Presentations.Count = 0 Then
        Set ppt = pp.Presentations.Add
    Else
        Set ppt = pp.ActivePresentation
    End If

    Set sld = ppt.Slides.Add(1, ppLayoutTitleOnly)
    Set shpTable = sld.Shapes.AddTable(rng.Rows.Count, rng.Columns.Count)
    For i = 1 To rng.Rows.Count
        For j = 1 To rng.Columns.Count
            shpTable.Table.Cell(i, j).Shape.TextFrame.TextRange.Text = _
                rng.Cells(i, j).Text
        Next
    Next

    For i = 1 To rng.Rows.Count
        For j = 1 To rng.Columns.Count
            If (rng.Cells(i, j).MergeArea.Cells.Count > 1) And _
                (rng.Cells(i, j).Text <> "") Then
                shpTable.Table.Cell(i, j).Merge _
                shpTable.Table.Cell(i + rng.Cells(i, j).MergeArea.Rows.Count - 1, _
                j + rng.Cells(i, j).MergeArea.Columns.Count - 1)
            End If
        Next
    Next

    sld.Shapes.Title.TextFrame.TextRange.Text = _
        rng.Worksheet.Name & " - " & rng.Address

End Sub
Judyjudye answered 9/11, 2012 at 16:37 Comment(0)
C
0
 ' Add table for column C headers
    Set myShape = mySlide.Shapes.AddTable(Rows:=1, Columns:=uniqueValuesC.Count, Left:=marginWidth, Top:=currentTop, Width:=ppPres.PageSetup.SlideWidth - 2 * marginWidth, Height:=shapeHeight)
    With myShape.Table
        ' Set table properties
        .TableStyle = "Table Grid"
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        .AutoFitBehavior (2) ' Auto-fit contents
        ' Add headers from unique values of column C
        For valueCounterC = 1 To uniqueValuesC.Count
            .Cell(1, valueCounterC).Shape.TextFrame.TextRange.Text = uniqueValuesC(valueCounterC)
            .Cell(1, valueCounterC).Shape.TextFrame.TextRange.Font.Size = 12 ' Set font size for table header
            .Cell(1, valueCounterC).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = 2 ' Center alignment horizontally
        Next valueCounterC
    End With
Christcrossrow answered 9/5 at 0:25 Comment(1)
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Bes

© 2022 - 2024 — McMap. All rights reserved.