【发布时间】: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