【问题标题】:Copying all charts from an Excel Sheet to a Powerpoint slide将所有图表从 Excel 工作表复制到 Powerpoint 幻灯片
【发布时间】:2017-03-16 21:04:13
【问题描述】:

我已经建立了一个工作簿来帮助创建我负责的月度报告演示文稿。工作簿有一些数据表、一些处理表和编号表,其中包含我需要粘贴到相应幻灯片的图表。到目前为止,我已经构建了 VBA,用于打开 PowerPoint 模板并循环遍历每个 Excel 工作表,并区分哪些工作表名称是数字,然后激活 PowerPoint 模板上的相应幻灯片。

与我发现的类似问题的其他解决方案不同,我想一次将每个编号表中的所有图表复制到每张幻灯片,因为每张表/幻灯片的形状、数量和配置都不同。我大多只发现人们一次复制一个图表并粘贴为图像,这对我也不起作用(我需要微调数据标签和最后一张幻灯片上的位置)。关于我如何实现这一点的任何提示?

到目前为止,我的代码如下所示:

Sub CriarSlides()

Dim pptApp As Powerpoint.Application
Dim pptPres As Powerpoint.Presentation
Dim strFileToOpen As Variant
Dim strFileName As String, Hosp As String
Dim datawb As Workbook
Dim xlsCounter As Integer, xlsSlide As Integer


Set datawb = ThisWorkbook


strFileToOpen = Application.GetOpenFilename _
FileFilter:="Powerpoint Files *.pptx (*.pptx),")
If strFileToOpen = False Then
   Exit Sub
Else
   Set pptApp = New Powerpoint.Application
   pptApp.Visible = True
   pptApp.Presentations.Open Filename:=strFileToOpen, ReadOnly:=msoFalse, Untitled:=msoTrue
   Set pptPres = pptApp.Presentations(1)
End If

For xlsCounter = datawb.Worksheets.Count To 1 Step -1
    If IsNumeric(datawb.Worksheets(xlsCounter).Name) Then
       xlsSlide = datawb.Worksheets(xlsCounter).Name

' This is the problematic part

        Debug.Print xlsSlide
    End If
Next xlsCounter
End Sub

【问题讨论】:

  • 1) 你如何确保工作表的名称与现有幻灯片相对应 2) 因为每张工作表有很多图表并且你不希望它们粘贴在同一张幻灯片中,你想如何处理那? 3) add 每个图表对象一张幻灯片不是更简单吗?
  • 我每张纸上确实有很多图表(最多 4 个),它们都必须粘贴在同一张幻灯片上。对不起,如果我不清楚这一点。下面是一个例子:一个工作表被命名为“8”,因为它上面的所有图表都应该粘贴在幻灯片 8 上。另外,我确保工作表的名称与现有的幻灯片相对应,因为幻灯片模板已经存在了很长时间,并且每个月都会手动粘贴图表。

标签: excel vba charts powerpoint


【解决方案1】:

使用以下修改后的代码,您可以将每个工作表的图表对象粘贴到相应的幻灯片中:

Sub CriarSlides()
    Dim pptApp As PowerPoint.Application, pptPres As PowerPoint.Presentation
    Dim strFileToOpen As Variant, sh As Worksheet, ch As ChartObject

    strFileToOpen = Application.GetOpenFilename(FileFilter:="Powerpoint Files *.pptx (*.pptx),")
    If strFileToOpen = False Then Exit Sub
    Set pptApp = New PowerPoint.Application
    pptApp.Visible = True
    Set pptPres = pptApp.Presentations.Open(fileName:=strFileToOpen, ReadOnly:=msoFalse)

    For Each sh In ThisWorkbook.Sheets
        If IsNumeric(sh.name) Then
            For Each ch In sh.ChartObjects
                ch.Copy
                With pptPres.Slides(CLng(sh.name)).Shapes.Paste
                    .Top = ch.Top
                    .Left = ch.Left
                    .Width = ch.Width
                    .Height = ch.Height
                End With
            Next
        End If
    Next
End Sub

【讨论】:

  • 谢谢你的回答,但是反过来不行:每张表都包含多个图表,可以同时粘贴在同一张幻灯片中,保持它们之间的相对位置.
  • 很好,核心循环完成了这项工作。由于我的图表区域是在没有考虑幻灯片标题的情况下确定的,因此必须在顶部添加 100 个点。感谢您的努力,真的很感激!粘贴到 PowerPoint 时保持源格式的任何提示?
  • @GabrielMenezes 欢迎您。我没有注意到格式的变化。您从格式更改了哪些格式方面的内容?我认为这本身值得一个问题,解释格式的变化。我一定会尽力提供帮助,但也许还有其他人有想法可以纠正它。
  • 好吧,我所说的格式只是指配色方案。一些,不是全部,甚至我用这段代码粘贴的 82 个图表中的大多数都没有将配色方案恢复为默认值。将模板的配色方案更改为我在工作簿上使用的配色方案并没有帮助。但是更改演示文稿的配色方案它生成之后再次正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多