【发布时间】:2017-05-22 12:12:10
【问题描述】:
我正在使用 EXcel VBA 使用自动脚本将数据从 excel 传输到 powerpoint 幻灯片。我正在尝试复制 Excel 工作表的 usedrange 并将其作为图像粘贴到第四张幻灯片的 powerpoint 模板中,然后从那里添加新幻灯片并将剩余的工作表复制到下一张幻灯片。
我当前使用的代码出现以下错误“öbject 变量或未设置块变量”
谁能给我推荐以下代码。
希望这一点得到清楚的解释。如果不是,请要求更多说明。
谢谢
Private Sub CommandButton2_Click()
Dim PP As PowerPoint.Application
Dim PPpres As PowerPoint.Presentation
Dim PPslide As Object
Dim PpShape As PowerPoint.Shape
Dim SlideTitle As String
Dim SlideNum As Integer
Dim WSrow As Long
Dim Sh As Shape
Dim Rng As Range
Dim myshape As Object
'Open PowerPoint and create new presentation
Set PP = GetObject(class, "PowerPoint.Application")
PP.Visible = True
PP.Presentations.Open FileName:=("\\C:\Users\Templates)"
'Specify the chart to copy and copy it
For Each WS In Worksheets
If (WS.Name) <> "EOS" Then
ThisWorkbook.Worksheets(WS.Name).Activate
ThisWorkbook.ActiveSheet.UsedRange.CopyPicture
'pSlide.Shapes.Paste
'Copy Range from Excel
Set Rng = ThisWorkbook.ActiveSheet.Range("A1:I8")
'Copy Excel Range
Rng.Copy
'Set PPslide = PPpres.Slides.Add(5, 33)
PP.ActiveWindow.View.GotoSlide (4)
Set PPslide = PPpres.Slides(4).Shapes.Paste
'Paste to PowerPoint and position
PPslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myshape = PPslide.Shapes(PPslide.Shapes.Count)
'Set position:
myshape.Left = 66
myshape.Top = 152
End If
Next
'Make PowerPoint Visible and Active
PowerPointApp.Visible = True
PowerPointApp.Activate
'Clear The Clipboard
Application.CutCopyMode = Falseenter code here`
End Sub
【问题讨论】:
-
请通过修复缩进来正确格式化您的代码,以便代码格式化,并删除双倍行距:)
-
并说出哪一行错误。
-
您好,感谢您的回复。!!从这一行设置 PPslide = PPpres.Slides(4).Shapes.Paste 我收到错误为“öbject 变量或未设置块变量”。 Excel 工作表的范围正在复制,但在打开 Powerpoint 模板后未粘贴到特定幻灯片(如前所述的第 4 张幻灯片)中。
-
我没有发现这个错误。我相信你需要在两条线上将
PowerPointApp.更改为PP.。此外,在代码的倒数第二行,您已将 2 行合并在一起。第一个应该是Application.CutCopyMode = False,第二个应该是'enter code here