在 VBA 中,有什么方法可以在不影响全局剪贴板的情况下将 Excel 范围复制到 PowerPoint?

In VBA, is there any way to copy an Excel range to PowerPoint without disturbing the global clipboard?

我需要 运行 使用 Excel 模板创建大量 PowerPoint 文件的自动化流程,以生成所需的表格和图表,并将其作为图像粘贴到 PowerPoint 中。

这个过程需要很长时间,我希望能够在 运行ning 期间做其他事情,所以我尽量避免使用剪切粘贴,这样剪贴板就可以用于其他事情,而进程 运行ning。如果我在 VBA 代码中使用剪切和粘贴,则全局剪贴板在 运行ning 时不起作用。

我设法通过将图表导出到位图文件来避免使用图表的剪贴板。然而,对于单元格范围似乎没有这样的方法。有没有办法解决这个问题。

你可以试试这个。它使用 "Camera" 这些天在 Excel 中几乎看不见的工具。

sheet1 上有一张空图表,其中包含一张链接图片(请参阅 SetUpChart 添加图片)。

更改图片的公式会更改其链接图像。此过程似乎不会影响剪贴板。

请注意,如果运行时带有图表的 sheet 未激活,您将得到错误的输出。

'Test the export
Sub ExportSomeRanges()
    ExportRangeNoCopy Sheet1.Range("B11:D24"), "c:\Temp\test.png"
    ExportRangeNoCopy Sheet1.Range("B5:C8"), "c:\Temp\test2.png"
    ExportRangeNoCopy Sheet2.Range("C8:K13"), "c:\Temp\test3.png"
End Sub

'the sheet with the chart container needs to be active...
Sub ExportRangeNoCopy(rng As Range, fName As String)
   With Sheet1.ChartObjects(1)
        'size the chart
        .Height = rng.Height
        .Width = rng.Width
        With .Chart.Shapes(1)
            'size the picture and set the source range
            .Top = 0
            .Left = 0
            'change the link to the source range
            .DrawingObject.Formula = "'" & rng.Parent.Name & "'!" & _
                                       rng.Address(True, True)
            .Width = rng.Width
            .Height = rng.Height
        End With
        DoEvents
        .Chart.Export fileName:=fName, FilterName:="png"
   End With
End Sub


'Set up a chart with a linked picture object (one-time task)
'could not figure out how to do this manually.  Cribbed from:
'https://www.ozgrid.com/forum/index.php?thread/5405-solved-camera-tool-how-to-use-it-in-code/
Sub SetUpChart()
 Dim rngcolor
 Set rngcolor = Range("B5:C8")
 rngcolor.Copy
 ActiveSheet.ChartObjects(1).Select
 With ActiveChart.Pictures.Paste
 .Formula = "=" & rngcolor.Address()
 End With
 Application.CutCopyMode = False
End Sub