【问题标题】:Generate a PowerPoint Automated via VBA from Excel Data, Charts, Comments通过 VBA 从 Excel 数据、图表、注释自动生成 PowerPoint
【发布时间】:2015-02-17 08:33:28
【问题描述】:

我想要实现的是通过 VBA 在 Excel 中使用数据、图表、cmets 制作自动 PowerPointPres。

有两件事我没有做:

  1. 我需要在 PPP 的一页上放置 4 个图表,而不是每页 1 个图表
  2. 我需要保留源格式,不要将图表粘贴为图片。

有人可以帮忙改正吗?

真的很感激!

    Sub CreatePowerPoint()

 '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 activeSlide As PowerPoint.Slide
        Dim cht As Excel.ChartObject

     'Look for existing instance
        On Error Resume Next
        Set newPowerPoint = GetObject(, "PowerPoint.Application")
        On Error GoTo 0

    'Let's create a new PowerPoint
        If newPowerPoint Is Nothing Then
            Set newPowerPoint = New PowerPoint.Application
        End If
    'Make a presentation in PowerPoint
        If newPowerPoint.Presentations.Count = 0 Then
            newPowerPoint.Presentations.Add
        End If

    'Show the PowerPoint
        newPowerPoint.Visible = True

    'Loop through each chart in the Excel worksheet and paste them into the PowerPoint
        For Each cht In ActiveSheet.ChartObjects

        'Add a new slide where we will paste the chart
            newPowerPoint.ActivePresentation.Slides.Add newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutText
            newPowerPoint.ActiveWindow.View.GotoSlide newPowerPoint.ActivePresentation.Slides.Count
            Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count)

        'Copy the chart and paste it into the PowerPoint as a Metafile Picture
            cht.Select
            ActiveChart.ChartArea.Copy
            activeSlide.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select

        'Set the title of the slide the same as the title of the chart
            activeSlide.Shapes(1).TextFrame.TextRange.Text = cht.Chart.ChartTitle.Text

        'Adjust the positioning of the Chart on Powerpoint Slide
            newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 15
            newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 125

            activeSlide.Shapes(2).Width = 200
            activeSlide.Shapes(2).Left = 505

        'If the chart is the "US" consumption chart, then enter the appropriate comments
            If InStr(activeSlide.Shapes(1).TextFrame.TextRange.Text, "US") Then
                activeSlide.Shapes(2).TextFrame.TextRange.Text = Range("J7").Value & vbNewLine
                activeSlide.Shapes(2).TextFrame.TextRange.InsertAfter (Range("J8").Value & vbNewLine)
        'Else if the chart is the "Renewable" consumption chart, then enter the appropriate comments
            ElseIf InStr(activeSlide.Shapes(1).TextFrame.TextRange.Text, "Renewable") Then
                activeSlide.Shapes(2).TextFrame.TextRange.Text = Range("J27").Value & vbNewLine
                activeSlide.Shapes(2).TextFrame.TextRange.InsertAfter (Range("J28").Value & vbNewLine)
                activeSlide.Shapes(2).TextFrame.TextRange.InsertAfter (Range("J29").Value & vbNewLine)
            End If

        'Now let's change the font size of the callouts box
            activeSlide.Shapes(2).TextFrame.TextRange.Font.Size = 16

        Next

    AppActivate ("Microsoft PowerPoint")
    Set activeSlide = Nothing
    Set newPowerPoint = Nothing

End Sub

【问题讨论】:

  • 试试 www.pptxbuilder.com

标签: excel powerpoint powerpoint-2010 vba


【解决方案1】:

我想您可以通过将幻灯片创建部分移出循环并将其第一部分更改为如下所示来实现:

    'Add a new slide where we will paste the chart
    newPowerPoint.ActivePresentation.Slides.Add newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutText
    newPowerPoint.ActiveWindow.View.GotoSlide newPowerPoint.ActivePresentation.Slides.Count
    Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count)

'Loop through each chart in the Excel worksheet and paste them into the PowerPoint
    For Each cht In ActiveSheet.ChartObjects

        Dim i As Integer
        i = i + 1

    'Copy the chart and paste it
        cht.Select
        ActiveChart.ChartArea.Copy
        activeSlide.Shapes.PasteSpecial(DataType:=ppPasteDefault).Select

    'Set the title of the slide the same as the title of the chart
    If cht.Chart.HasTitle = True Then
        activeSlide.Shapes(1).TextFrame.TextRange.Text = cht.Chart.ChartTitle.Text
    End If

    'Adjust the positioning of the Chart on Powerpoint Slide
    If i = 1 Then
        newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 5
        newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 100

        ElseIf i = 2 Then
        newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 350
        newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 100

        ElseIf i = 3 Then
        newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 5
        newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 325

        Else
        newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 350
        newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 325
    End If

然后,您可能需要进行一些调整。

【讨论】:

  • 请更正您的姓名:)。我仍在努力——正如你所说,需要进行一些调整。
猜你喜欢
  • 2014-09-23
  • 1970-01-01
  • 1970-01-01
  • 2011-09-15
  • 2017-08-30
  • 2017-06-15
  • 2011-07-18
  • 1970-01-01
  • 2021-07-17
相关资源
最近更新 更多