【问题标题】:Copy Excel charts and tables to Powerpoint将 Excel 图表和表格复制到 Powerpoint
【发布时间】:2015-03-10 17:30:16
【问题描述】:

我正在尝试在 excel 中创建图表和表格,然后通过 PowerPoint VBA 宏将它们复制到 PowerPoint 中的幻灯片。我创建了图表和表格,但在复制和粘贴它们时遇到问题。我不熟悉这样做的语法。任何帮助将不胜感激,因为我是 PowerPoint VBA 的新手。

Sub GenerateVisual()

    Dim dlgOpen As FileDialog
    Dim folder As String
    Dim excelApp As Object
    Dim xlWorkBook As Object
    Dim xlWorkBook2 As Object
    Dim PPT As Presentation
    Dim Name1 As String
    Dim Name2 As String

    Set PPT = ActivePresentation

    Set excelApp = CreateObject("Excel.Application")

    excelApp.Visible = True


    Set xlWorkBook = excelApp.workbooks.Open("C:\Users\wzawisa\Downloads\MarketSegmentTotals.xls")
    xlWorkBook.Sheets("MarketSegmentTotals").Activate
    xlWorkBook.ActiveSheet.Shapes.AddChart.Select
    xlWorkBook.ActiveChart.ChartType = xlColumnClustered
    xlWorkBook.ActiveChart.SetSourceData Source:=xlWorkBook.ActiveSheet.Range("MarketSegmentTotals!$A$1:$F$2")
    xlWorkBook.ActiveChart.Legend.Delete
    xlWorkBook.ActiveChart.SetElement (msoElementChartTitleAboveChart)
    xlWorkBook.ActiveChart.SetElement (msoElementDataLabelCenter)
    xlWorkBook.ActiveChart.ChartTitle.Text = "DD Ready by Market Segment"
    xlWorkBook.ActiveSheet.ListObjects.Add

    xlWorkBook.ActiveSheet.ChartObjects(1).Select 'My attempt to copy them over but it doesnt work
    PPT.ActiveWindow.View.Paste

End Sub

【问题讨论】:

    标签: vba excel powerpoint copy-paste


    【解决方案1】:

    这个潜艇会带你上路。它需要一些调整,但这可以在一定范围内复制到 PPT 中:

    Public Sub RangeToPresentation(sheetName, NamedRange)
        Dim CopyRng As Range
    
        Set CopyRng = Sheets(sheetName).Range(NamedRange)
    
        Dim ppApp As Object
        Dim ppPres As Object
        Dim PPSlide As Object
    
        If Not TypeName(CopyRng) = "Range" Then
            MsgBox "Please select a worksheet range and try again.", vbExclamation, _
                "No Range Selected"
        Else
    
            Set ppApp = GetObject(, "Powerpoint.Application")
    
        Set ppPres = ppApp.ActivePresentation
        ppApp.ActiveWindow.ViewType = ppViewNormal
    
            Dim longSlideCount As Long
    
          ' Determine how many slides are in the presentation.
          longSlideCount = ppPres.Slides.Count
    
          With ppPres
    
             ' Insert a slide at the end of the presentation
             Set PPSlide = ppPres.Slides.Add(longSlideCount + 1, ppLayoutBlank)
    
          End With
    
        ' Select the last (blank slide)
        longSlideCount = ppPres.Slides.Count
        ppPres.Slides(longSlideCount).Select
    
        Set PPSlide = ppPres.Slides(ppApp.ActiveWindow.Selection.SlideRange.SlideIndex)
    
        CopyRng.CopyPicture Appearance:=xlScreen, _
            Format:=xlBitmap
    
        ' Paste the range
        PPSlide.Shapes.Paste.Select
    
        'Set the image to lock the aspect ratio
        ppApp.ActiveWindow.Selection.ShapeRange.LockAspectRatio = msoTrue
    
        'Set the image size slightly smaller than width of the PowerPoint Slide
        ppApp.ActiveWindow.Selection.ShapeRange.Width = ppApp.ActivePresentation.PageSetup.SlideWidth - 10
        ppApp.ActiveWindow.Selection.ShapeRange.Height = ppApp.ActivePresentation.PageSetup.SlideHeight - 10
    
        'Shrink image if outside of slide borders
        If ppApp.ActiveWindow.Selection.ShapeRange.Width > 700 Then
        ppApp.ActiveWindow.Selection.ShapeRange.Width = 700
        End If
    
        If ppApp.ActiveWindow.Selection.ShapeRange.Height > 600 Then
        ppApp.ActiveWindow.Selection.ShapeRange.Height = 600
        End If
    
        ' Align the pasted range
        ppApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
        ppApp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
    
    
        ' Clean up
        Set PPSlide = Nothing
        Set ppPres = Nothing
        Set ppApp = Nothing
        End If
    
    End Sub
    

    【讨论】:

    • @Pablo 我不明白你的问题。您是在问从哪里运行此代码?
    • 是的。它看起来像是从excel运行的。我需要从 powerpoint 运行它。
    • @Pablo 在我的代码中的所有 Range 引用前面使用您的 xlWorkBook 对象以从 PPT 运行。
    • 使用图表覆盖的范围。这将将该区域复制为图片(将抓取图表)。我可以寻找更好的语法来抓取图表,但这将解决您现在的需求。
    • @Pablo 此代码创建一个新幻灯片并将其粘贴到该新幻灯片。但是代码ppPres.Slides(longSlideCount).Select可以改成ppPres.Slides(insert number of slide here).Select
    猜你喜欢
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 2015-02-06
    • 2020-12-21
    • 2012-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多