简体   繁体   English

使用 VBA 将 Excel 中的范围粘贴到 Powerpoint 模板的特定幻灯片中

[英]Paste a range from Excel into certain slide of Powerpoint Template using VBA

I've spent a good few hours looking at various suggested solutions to my problem but cannot find anything that seems to do the job, or more likely, my grasp of VBA is understanding my ability to make sense of the online solutions.我花了好几个小时查看针对我的问题的各种建议解决方案,但找不到任何似乎可以完成工作的东西,或者更有可能的是,我对 VBA 的掌握是了解我理解在线解决方案的能力。 As such, I'm hoping one of you kind folk can help me resolve things.因此,我希望你们中的一位能帮助我解决问题。

I have an Excel Worksheet open that contains a small amount of data (Headings and numbers etc.)我打开了一个 Excel 工作表,其中包含少量数据(标题和数字等)

I want to copy this range and paste it (not as a picture, I want to be able to format it within powerpoint if necessary) on a particular slide (slide 2) in a Powerpoint template I have already created.我想复制此范围并将其粘贴(不是作为图片,如果需要,我希望能够在 powerpoint 中对其进行格式化)在我已经创建的 Powerpoint 模板中的特定幻灯片(幻灯片 2)上。

My VBA correctly opens up a new presentation based on the template using the following code:我的 VBA 使用以下代码正确打开基于模板的新演示文稿:

    'Opens a PowerPoint Document from Excel

Dim objPPT As Object

Set objPPT = CreateObject("PowerPoint.Application")
objPPT.Visible = True

'Change the directory path and file name to the location
'of your document

objPPT.Presentations.Open "C:\Users\Colin\Dropbox (Edge45)\Edge45 Team Folder\Edge45 Company Documents\Templates\Powerpoint Templates\Edge45 Audit Template Macro.potm", Untitled:=msoTrue

However, this is where I get stuck - I don't know how to paste the Excel tabular data into the correct slide once the presentation is open.但是,这就是我卡住的地方 - 我不知道如何在演示文稿打开后将 Excel 表格数据粘贴到正确的幻灯片中。 My data is in range A1:D16我的数据在 A1:D16 范围内

Thank you so much in advance非常感谢你提前

Try out this one.试试这个。 I have assigned Excel worksheet to one variable ( XLws ) and PowerPoint slide to another ( PPslide ).我已将 Excel 工作表分配给一个变量 ( XLws ),并将 PowerPoint 幻灯片分配给另一个变量 ( PPslide )。 You can Copy/Paste objects between them.您可以在它们之间复制/粘贴对象。

Btw, you need to have PowerPoint Library enabled.顺便说一句,您需要启用 PowerPoint 库。 If you don't, in your VBA project go to Tools -> References -> Microsoft PowerPoint 15.0 Object Library .如果不这样做,请在 VBA 项目中转到Tools -> References -> Microsoft PowerPoint 15.0 Object Library

Sub PPcopy()
Dim PPapp As PowerPoint.Application, PPpres As PowerPoint.Presentation, PPslide As PowerPoint.Slide
Dim XLws As Worksheet

    Set XLws = ActiveSheet
    Set PPapp = New PowerPoint.Application
    Set PPpres = PPapp.Presentations.Open("C:\Users\Colin\Dropbox (Edge45)\Edge45 Team Folder\Edge45 Company Documents\Templates\Powerpoint Templates\Edge45 Audit Template Macro.potm")
    PPapp.Visible = True
    Set PPslide = PPpres.Slides(2)
    XLws.Range("A1:D16").Copy
    PPslide.Shapes.PasteSpecial DataType:=ppPasteOLEObject, Link:=msoFalse
    Application.CutCopyMode = False

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM