繁体   English   中英

VBA PowerPoint 格式

[英]VBA Powerpoint formatting

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

我不断收到以下错误:

Selection.ShapeRange:无效请求。 当前未选择任何合适的内容

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

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

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

取而代之的是:

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

暂无
暂无

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

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