简体   繁体   English

Excel VBA:将Excel范围粘贴为Powerpoint中的表

[英]Excel VBA: Paste Excel Range as a Table in Powerpoint

I'm trying to automate the creation of powerpoint decks that i have to produce every month. 我正在尝试自动创建每个月必须制作的PowerPoint卡座。 I'm working in Excel VBA and cant figure out how to copy a range from excel, and paste it into a slide as a table. 我正在使用Excel VBA,无法弄清楚如何从excel复制范围并将其粘贴到表格中。

Below is the code i have so far: 下面是我到目前为止的代码:

Sub Open_PowerPoint_Presentation()

Dim objPPT As Object, _
PPTPrez As PowerPoint.Presentation, _
pSlide As PowerPoint.Slide

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

Set PPTPrez = objPPT.Presentations.Open("file location")

Set pSlide = PPTPrez.Slides(4)

Dim RevenueDetail As Range
Dim RevenueDetailTable As Object

Sheets("Revenue By Type Slide").Activate

Set RevenueDetail = Range("B4:I18")
RevenueDetail.Copy

Set RevenueDetailTable = pSlide.Shapes.PasteSpecial(ppPasteEnhancedMetafile)

With RevenueDetailTable

.Left = 43.99961
.Top = 88.61086
.Width = 471.2827
.Height = 395.2163

End With

End Sub

This works OK but it pastes the excel range as a picture which is not ideal. 效果很好,但是将excel范围粘贴为不理想的图片。 i'd like to paste it as a table which is what the default paste option does, but then i lose the ability to re-size and re-position it on the slide by the means that i'm currently using. 我想将其粘贴为表格,这是默认粘贴选项的作用,但随后我无法通过当前使用的方式来调整大小并将其重新放置在幻灯片上。 I've been messing with this for awhile and can't seem to get it right. 我已经弄了好一阵子,似乎无法正确解决。

if i modify 如果我修改

Set RevenueDetailTable = pSlide.Shapes.PasteSpecial(ppPasteEnhancedMetafile)

and change it to 并将其更改为

Set RevenueDetailTable = pSlide.Shapes.Paste

it pastes in the format i want but i cant figure out how to reposition and resize. 它以我想要的格式粘贴,但我无法弄清楚如何重新定位和调整大小。 any help would be greatly appreciated. 任何帮助将不胜感激。

In your code change: 在您的代码更改中:

Set RevenueDetailTable = pSlide.Shapes.PasteSpecial(ppPasteEnhancedMetafile)

With RevenueDetailTable

.Left = 43.99961
.Top = 88.61086
.Width = 471.2827
.Height = 395.2163

End With

Into: 成:

pSlide.Shapes.Paste
For Each SHP In pSlide.Shapes
    If SHP.Type = 14 Then
        SHP.Left = 43.99961
        SHP.Top = 88.61086
        SHP.Width = 471.2827
        SHP.Height = 395.2163
    End If
Next

Obviously have SHP dimmed as Object 显然SHP变暗为Object

Fixed it... just needed to add a line "pSlide.Select" to select the slide i'm pasting into prior to pasting, and change .PasteSpecial(ppPasteEnhancedMetafile) to just .Paste...thanks for all help!!!! 修复它...只需在粘贴之前添加一行“ pSlide.Select”以选择我要粘贴的幻灯片,然后将.PasteSpecial(ppPasteEnhancedMetafile)更改为.Paste ...感谢所有帮助! !

Sub Open_PowerPoint_Presentation()

Dim objPPT As Object, _
PPTPrez As PowerPoint.Presentation, _
pSlide As PowerPoint.Slide

Dim RevenueDetail As Range
Dim RevenueDetailTable As Object

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

Set PPTPrez = objPPT.Presentations.Open("file location")

Set pSlide = PPTPrez.Slides(4)

Set RevenueDetail = Sheets("Revenue By Type Slide").Range("B4:I18")
RevenueDetail.Copy

pSlide.Select 'needed to add this line
Set RevenueDetailTable = pSlide.Shapes.Paste

With RevenueDetailTable

.Left = 43.99961
.Top = 88.61086
.Width = 471.2827
.Height = 395.2163

End With

End Sub

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

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