让我们把这个问题分成几个不同的部分:
- 创建 PowerPoint 应用程序
- 复制图表粘贴
- 图表作为正确的格式。
现在查看您的代码,您可以继续使用前两个代码。它正在粘贴导致问题的对象。让我们探索不同的粘贴方式。
使用 EXECUTEMSO 方法:
当我们使用这种方法时,就像我们在幻灯片上单击鼠标右键并将对象粘贴到幻灯片上一样。现在,虽然这种方法是一种完全有效的粘贴方式,但在 VBA 中实现这一点可能有点挑战。原因是它非常不稳定,我们必须把我们的脚本放慢到蜗牛的速度!
要实现此方法及其任何不同选项,请执行以下操作:
'Create a new slide in the Presentation, set the layout to blank, and paste range on to the newly added slide.
Set PPTSlide = PPTPres.Slides.Add(1, ppLayoutBlank)
'WARNING THIS METHOD IS VERY VOLATILE, PAUSE THE APPLICATION TO SELECT THE SLIDE
For i = 1 To 5000: DoEvents: Next
PPTSlide.Select
'WARNING THIS METHOD IS VERY VOLATILE, PAUSE THE APPLICATION TO PASTE THE OBJECT
For i = 1 To 5000: DoEvents: Next
PPTApp.CommandBars.ExecuteMso "PasteSourceFormatting"
PPTApp.CommandBars.ReleaseFocus
'PASTE USING THE EXCUTEMSO METHOD - VERY VOLATILE
'Paste As Source Formatting
'PPTApp.CommandBars.ExecuteMso "PasteSourceFormatting"
'Paste as Destination Theme
'PPTApp.CommandBars.ExecuteMso "PasteDestinationTheme"
'Paste as Embedded Object
'PPTApp.CommandBars.ExecuteMso "PasteAsEmbedded"
'Paste Excel Table Source Formatting
'PPTApp.CommandBars.ExecuteMso "PasteExcelTableSourceFormatting"
'Paste Excel Table Destination Theme
'PPTApp.CommandBars.ExecuteMso "PasteExcelTableDestinationTableStyle"
现在,如果您查看我的代码,我必须将其暂停两次以确保它可以正常工作。这是因为 VBA 会移动得太快,否则会发生的只是它将所有对象粘贴到第一张幻灯片上!如果我们只做一个粘贴,我们通常是安全的,没有暂停,但是当你想去一张新幻灯片的那一刻,暂停!
使用常规粘贴方法:
当我们使用这种方法时,就像我们按 Crtl+V 一样,它会简单地将对象粘贴为 PowerPoint 中的常规形状。常规形状表示 PowerPoint 中的默认粘贴类型。下面是我们如何实现一个简单的粘贴方法:
'PASTE USING PASTE METHOD - NOT AS VOLATILE
'Use Paste method to Paste as Chart Object in PowerPoint
PPTSlide.Shapes.Paste
使用粘贴特殊方法:
当我们使用这种方法时,就像我们在键盘上按Ctrl++Alt一样V,我们会得到各种不同的粘贴方式。它的范围从图片一直到嵌入对象,我们可以链接回源工作簿。
使用 paste 特殊方法,有时我们仍然需要暂停我们的脚本。原因就像我上面提到的原因一样,VBA 是不稳定的。仅仅因为我们复制它并不意味着它会进入我们的剪贴板。这个问题可能会同时弹出然后消失,所以我们最好的办法是在我们的脚本中暂停一下,让 VBA 有足够的时间将信息放入剪贴板。它通常不必是长时间的停顿,而只需一两秒。以下是我们如何使用我们可以使用的不同选项来实现特殊粘贴方法:
'PASTE USING PASTESPECIAL METHOD - NOT AS VOLATILE
'Paste as Bitmap
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteBitmap
'Paste as Default
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteDefault
'Paste as EnhancedMetafile
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteEnhancedMetafile
'Paste as HTML - DOES NOT WORK WITH CHARTS
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteHTML
'Paste as GIF
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteGIF
'Paste as JPG
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteJPG
'Paste as MetafilePicture
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteMetafilePicture
'Paste as PNG
PPTSlide.Shapes.PasteSpecial DataType:=ppPastePNG
'Paste as Shape
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteShape
'Paste as Shape, display it as an icon, change the icon label, and make it a linked icon.
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteShape, DisplayAsIcon:=True, IconLabel:="Link to my Chart", Link:=msoTrue
'Paste as OLEObject and it is linked.
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteOLEObject, Link:=msoFalse
话虽如此,如果您将对象粘贴为带有链接的 OLEObject,则大多数时候格式都会随之而来。除非你有一个只存在于 Excel 中的特殊主题,否则你就会遇到麻烦。当我从 Excel 到 Word 中获取图表时遇到了这个问题,但是 Excel 图表有一个自定义主题。
这是您的代码,已重写,以便它将使用源格式粘贴对象并设置其尺寸。我希望您不介意我重新调整您的一些代码以使其更简洁。
Sub PasteRangeIntoPowerPoint()
'Declare your variables
Dim oPPTApp As PowerPoint.Application
Dim oPPTFile As PowerPoint.Presentation
Dim oPPTShape As PowerPoint.Shape
Dim oPPTSlide As PowerPoint.Slide
Dim Rng As Range
'Get the PowerPoint Application, I am assuming it's already open.
Set oPPTApp = GetObject(, "PowerPoint.Application")
'Set a reference to the range you want to copy, and then copy it.
Set Rng = Worksheets("Sheet1").Range("B3:N9")
Rng.Copy
'Set a reference to the active presentation.
Set oPPTFile = oPPTApp.ActivePresentation
'Set a reference to the slide you want to paste it on.
Set oPPTSlide = oPPTFile.Slides(3)
'WARNING THIS METHOD IS VERY VOLATILE, PAUSE THE APPLICATION TO SELECT THE SLIDE
For i = 1 To 5000: DoEvents: Next
oPPTSlide.Select
'WARNING THIS METHOD IS VERY VOLATILE, PAUSE THE APPLICATION TO PASTE THE OBJECT
For i = 1 To 5000: DoEvents: Next
oPPTApp.CommandBars.ExecuteMso "PasteSourceFormatting"
oPPTApp.CommandBars.ReleaseFocus
For i = 1 To 5000: DoEvents: Next
'Set the dimensions of your shape.
With oPPTApp.ActiveWindow.Selection.ShapeRange
.Left = 35
.Top = 150
End With
End Sub