【问题标题】:VBA Powerpoint formattingVBA PowerPoint 格式
【发布时间】:2019-12-15 19:10:41
【问题描述】:

我编写了一段代码来从 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

【问题讨论】:

    标签: excel vba powerpoint


    【解决方案1】:

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

    而不是这个:

    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
    

    【讨论】:

    • 太棒了!这非常有效!图片的粘贴(数据类型 2)现在又可以正常工作了,您可能知道我怎样才能让常规粘贴工作再次正常工作吗?我在 2016 年使用过,现在在 Excel 2010 中不再适用了。PPSlide.Shapes.Paste.Select
    • 通常你要避免选择任何东西,除非没有其他办法。在这种情况下,您可以尝试 PPSlide.Shapes.Paste(可能添加一些 DoEvents),然后设置 oSh = PPSlide.Shapes(PPSlide.Shapes.Count)(首先将 oSh 调暗为 Shape)。
    猜你喜欢
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多