vba 将 Excel 表格复制到 PowerPoint (2010) 的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25558354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:24:49  来源:igfitidea点击:

Best Way to Copy Excel Table into PowerPoint (2010)?

excelvbaexcel-vbapowerpointpowerpoint-vba

提问by

I'm trying to get a series of Excel tables into PowerPoint and successfully created a macro for this in Office 2013, but am trying to adapt it to Office 2010.

我正在尝试将一系列 Excel 表格放入 PowerPoint 并在 Office 2013 中成功为此创建了一个宏,但我正在尝试使其适应 Office 2010。

The issue is when pasting the table to PowerPoint, Office 2010 seems to require a unique/different code.

问题是将表格粘贴到 PowerPoint 时,Office 2010 似乎需要唯一/不同的代码。

Originally I had:

最初我有:

'Copying Tables to PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = ppViewSlide

For i = 0 To Table3
    Sheets("Charts").Range(ChartStart, ChartEnd).Offset(i * Row2, 0).Copy
    Set PPSlide = PPPres.Slides(1)
    Set PPShape = PPSlide.Shapes.Paste
    PPShape.Name = "Table" & i

But have since been informed a fix for 2010 versions is to use .PasteSpecial so I have:

但是后来被告知 2010 版本的修复程序是使用 .PasteSpecial 所以我有:

'Copying Tables to PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = ppViewSlide

For i = 0 To Table3
    Sheets("Charts").Range(ChartStart, ChartEnd).Offset(i * Row2, 0).Copy
    Set PPSlide = PPPres.Slides(1)
    Set PPShape = PPSlide.Shapes.PasteSpecial(dataType:=10)
    PPShape.Name = "Table" & i

The thing is using DataType:=10 puts the chart into PowerPoint in an incorrect format for my purposes. I need to get the table into PowerPoint as a table using ppPasteHTML, however, when I try to enter this into the PasteSpecial function I get an error code saying the Clip board is empty.

事情是使用 DataType:=10 将图表以不正确的格式放入 PowerPoint 中以符合我的目的。我需要使用 ppPasteHTML 将表格作为表格放入 PowerPoint,但是,当我尝试将其输入 PasteSpecial 函数时,我收到错误代码,指出剪贴板为空。

Does anyone know if "ppPasteHTML' has a numeric equivalent for the DataType option in PasteSpecial? Or another way to get an Excel table into PowerPoint as a table for Office 2010?

有谁知道“ppPasteHTML”是否具有与 PasteSpecial 中的 DataType 选项等效的数字?或者另一种将 Excel 表格作为 Office 2010 表格导入 PowerPoint 的方法?

Thanks!

谢谢!

回答by CaptainABC

This should do it:

这应该这样做:

Just make sure you are loading the PowerPoint Library in Excel.

只需确保您正在 Excel 中加载 PowerPoint 库。

Tools->References->"Microsoft PowerPoint nn.n Object Library"

工具->参考->“Microsoft PowerPoint nn.n 对象库”

Also I assume that Table3, ChartStart, ChartEnd& Row2have set values

另外我假设Table3, ChartStart, ChartEnd&Row2已经设置了值

Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSlide As PowerPoint.Slide

'Open PowerPoint and create a new presentation.
Set pptApp = New PowerPoint.Application
Set pptPres = pptApp.Presentations.Add

Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)

For i = 0 To Table3
Set objRange = Worksheets("Charts").Range(ChartStart, ChartEnd).Offset(i * Row2, 0)
objRange.Copy
pptSlide.Shapes.PasteSpecial DataType:=ppPasteHTML, Link:=msoFalse
Next i

For j = 1 To pptSlide.Shapes.Count
    With pptSlide.Shapes(j)
    .Name = "Table" & j
    End With
Next j

Set pptSlide = Nothing
Set pptPres = Nothing
Set pptApp = Nothing

回答by David Zemens

The long/numeric equivalent for ppPasteHtmlis 8. You can query this for yourself by opening up the VBE in PowerPoint, and doing ?ppPasteHTMLin the Immediate window, or Debug.Print ppPasteHtmlin a module/routine.

的长/数字等价物ppPasteHtml8。您可以通过在 PowerPoint 中打开 VBE 并?ppPasteHTML在立即窗口或Debug.Print ppPasteHtml模块/例程中进行查询来自行查询。

Using early binding, try:

使用早期绑定,尝试:

pptSlide.Shapes.PasteSpecialy DataType:=ppPasteHtml

Or, using late binding:

或者,使用后期绑定:

pptSlide.Shapes.PasteSpecial DataType:=8

Alternatively, I have seen a few other q's where people have problems pasting from one application to another application(e.g., from Excel to PowerPoint etc.) In those cases, it seems that sometimes the only way to resolve is to use the CommandBarsobject, however I am not sure if there is an "HTML" paste method from CommandBars.

或者,我还看到了其他一些问题,其中人们在从一个应用程序粘贴到另一个应用程序(例如,从 Excel 到 PowerPoint 等)时遇到问题。在这些情况下,似乎有时唯一的解决方法是使用CommandBars对象,但是我不确定是否有来自 CommandBars 的“HTML”粘贴方法。

pptSlide.Parent.ExecuteMso "PasteExcelTableSourceFormatting"

Here are some other possible MSO commands you could use, but like I said I don't see one that appears to paste HTML, although this list is for Office 2010:

以下是您可以使用的其他一些可能的 MSO 命令,但正如我所说,我没有看到似乎粘贴 HTML 的命令,尽管此列表适用于 Office 2010:

enter image description here

在此处输入图片说明