VBA Powerpoint格式化

时间:2017-12-13 17:12:18

标签: excel vba powerpoint

我已经编写了一段代码来复制excel中的数据并将其粘贴到powerpoint演示文稿中。

我经常收到以下错误:

  

Selection.ShapeRange:无效的请求。什么都不合适   目前已选中

这与代码的以下部分有关(我将Excel数据粘贴到powerpoint幻灯片并确定其位置)。

PPSlide.Shapes.PasteSpecial DataType:=2
 pp.ActiveWindow.Selection.ShapeRange.Top = 0
 pp.ActiveWindow.Selection.ShapeRange.Left = 0
 pp.ActiveWindow.Selection.ShapeRange.Width = 1000

奇怪的是,这段代码曾在几周前(excel 2016)工作,但从今天开始(我被降级为Excel 2010)它突然停止工作..

我使用的完整代码如下:

 'Step 1:  Declare variables

       Dim pp As Object
       Dim PPPres As Object
       Dim PPSlide As Object
       Dim xlwksht As Worksheet
       Dim MyRange As String
       Dim MyRange2 As String
       Dim TemplatePath As String

 'Step 2:  Open PowerPoint, add a new presentation and make visible

       Set pp = CreateObject("PowerPoint.Application")
       Set PPPres = pp.Presentations.Add
       pp.Visible = True

 'Step 3:  Start the loop through each worksheet

       'Step 3-A: Skip Excel sheets 1 till 8
       For Each xlwksht In Worksheets
       If xlwksht.Index >= 9 Then

       'Step 3-B:  Count slides and add new blank slide as next available slide number
       SlideCount = PPPres.Slides.Count
       Set PPSlide = PPPres.Slides.Add(SlideCount + 1, 12)
       PPSlide.Select

 'Step 4:    Copy the Content section from Excel

       MyRange = xlwksht.Range("A1").Value & ":" & xlwksht.Range("A2").Value
       xlwksht.Range(MyRange).Copy

 'Step 5:  Paste the Content and adjust its position

       'Step 5-A: Determine the Path of the Template and apply it to the Powerpoint presentation

       PPPres.ApplyTemplate (TemplatePath & "\Template.potx")

       'Step 5-B: Determine the PasteType
       pastetype = xlwksht.Range("C1").Value  'Where C1 = "Image" for all images and tables
       PasteWidth = xlwksht.Range("D1").Value 'Where D1 = "Title" then picture will fill whole screen

       'Step 5-C: Based on the Pastetype paste the content in the presentation
       If pastetype = "Image" Then
             If PasteWidth = "Title" Then

                   'Step 5-C-1  Format only for Title Page
                    PPSlide.Shapes.PasteSpecial DataType:=2
                     pp.ActiveWindow.Selection.ShapeRange.Top = 0
                     pp.ActiveWindow.Selection.ShapeRange.Left = 0
                     pp.ActiveWindow.Selection.ShapeRange.Width = 1000
             Else

                   'Step 5-C-2  Format for Images
                    PPSlide.Shapes.PasteSpecial DataType:=2
                     pp.ActiveWindow.Selection.ShapeRange.Top = 95
                     pp.ActiveWindow.Selection.ShapeRange.Left = 20
                     pp.ActiveWindow.Selection.ShapeRange.Width = 300
             End If
       Else

             'Step 5-C-3  Format for Normal tables
              PPSlide.Shapes.Paste.Select
              pp.ActiveWindow.Selection.ShapeRange.Top = 95
              pp.ActiveWindow.Selection.ShapeRange.Left = 20
       End If

1 个答案:

答案 0 :(得分:0)

这可能是也可能不是答案,但在此处插入建议将使其比将其作为评论更加清晰:

而不是:

PPSlide.Shapes.PasteSpecial DataType:=2
   pp.ActiveWindow.Selection.ShapeRange.Top = 0
   pp.ActiveWindow.Selection.ShapeRange.Left = 0
   pp.ActiveWindow.Selection.ShapeRange.Width = 1000

试试这个:

With PPSlide.Shapes.PasteSpecial DataType:=2
  .Top = 0
  .Left = 0
  .Width = 1000
End With