在VBA中将范围从Excel复制到PPT

时间:2015-01-15 20:03:12

标签: excel vba excel-vba powerpoint powerpoint-vba

我正在尝试使用下面的Powerpoint宏将excel中的范围复制并粘贴到ppt作为表格。出于某种原因,它不允许我按照我编写代码的方式粘贴ppt,我很困惑为什么。对此的任何帮助将不胜感激。

Public Sub RangeToPresentation(ByVal sheetName As String, NamedRange As Excel.Range)
    Dim ppApp As Object
    Dim ppPres As Object
    Dim PPSlide As Object

    Set ppApp = GetObject(, "Powerpoint.Application")

    Set ppPres = ppApp.ActivePresentation
    ppApp.ActiveWindow.ViewType = ppViewNormal

    ' Select the last (blank slide)
    Dim longSlideCount As Integer
    longSlideCount = ppPres.Slides.Count
    ppPres.Slides(1).Select

    Set PPSlide = ppPres.Slides(ppApp.ActiveWindow.Selection.SlideRange.SlideIndex)


    NamedRange.Copy

    ' Paste the range
    PPSlide.Paste.Select
End Sub

1 个答案:

答案 0 :(得分:0)

尝试替换它:

 ppApp.ActiveWindow.ViewType = ppViewNormal

' Select the last (blank slide)
Dim longSlideCount As Integer
longSlideCount = ppPres.Slides.Count
ppPres.Slides(1).Select

Set PPSlide = ppPres.Slides(ppApp.ActiveWindow.Selection.SlideRange.SlideIndex)


NamedRange.Copy

' Paste the range
PPSlide.Paste.Select

有了这个:

 ppApp.ActiveWindow.ViewType = ppViewNormal

' No need to select the slide
' And slide indexes are longs, not integers
Dim longSlideCount As Long
longSlideCount = ppPres.Slides.Count

NamedRange.Copy

' Paste the range; don't select it.
ppPres.Slides(ppPres.Slides.Count).Shapes.Paste

这是最重要的,但试一试,让我们知道它是如何工作的