vba 修改Excel VBA代码粘贴到特定ppt幻灯片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18664244/
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
Modify Excel VBA code to paste into specific ppt slides
提问by Doug
I am new to the forum, and a few months new to VBA. All my coding experience is in Matlab, which so far I find much easier than VBA. I have read the forum rules, and searched for this answer high and low, but to no avail.
我是论坛的新手,也是 VBA 的新手。我所有的编码经验都在 Matlab 中进行,到目前为止我发现它比 VBA 容易得多。我已经阅读了论坛规则,并高低搜索了这个答案,但无济于事。
I am trying to paste different excel ranges into different slides of a PowerPoint. I want to modify the code below (taken in snipets from different forum answers) so that the range is pasted into the corresponding PowerPoint slide I denote, regardless of what the active slide is.I have tried "gotoslide" and that didn't work, and used ppviewnormal, but that also didn't work. This code will then be repeated for different ranges on different slides. So far the code will only work when I am on the slide it was written for, and I know it has to do with issues regarding the active slide, but cannot seem to figure out how to modify the code. Thank you in advance for your help!
我正在尝试将不同的 excel 范围粘贴到 PowerPoint 的不同幻灯片中。我想修改下面的代码(取自不同论坛答案的片段),以便将范围粘贴到我表示的相应 PowerPoint 幻灯片中,而不管活动幻灯片是什么。我试过“gotoslide”但没有用,并使用了ppviewnormal,但这也没有用。然后将针对不同幻灯片上的不同范围重复此代码。到目前为止,代码仅在我为它编写的幻灯片上时才有效,我知道它与有关活动幻灯片的问题有关,但似乎无法弄清楚如何修改代码。预先感谢您的帮助!
' Desired Range in Excel to be copied into ppt
Sheets("Summary").Activate
Range("A5:H40").Select
' Initialize PowerPoint Object Library
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = ppViewSlide
' Denote slide where range is to be copied
Set PPSlide = PPPres.Slides(5)
' Copy the range as a picture
Selection.CopyPicture Appearance:=xlScreen, _
Format:=xlPicture
' Paste the range
PPSlide.Shapes.Paste.Select
回答by Piotr Marczewski
Remove ".Select" from last line
从最后一行删除“.Select”
This is shorter version of your code:
这是您的代码的较短版本:
' Initialize PowerPoint Object Library
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
' Copy the range as a picture
Sheets("Summary").Range("A5:H40").CopyPicture Appearance:=xlScreen, Format:=xlPicture
' Paste the range
PPPres.Slides(1).Shapes.Paste