2017-05-22 198 views
0

我使用EXcel VBA通​​过自动脚本将数据从excel传输到powerpoint幻灯片。我试图复制excel工作表的usedrange,然后将其粘贴为幻灯片中的图像,然后从第四张幻灯片的模板中添加新幻灯片并将其余工作表复制到下一张幻灯片。从VBA excel复制到powerpoint

这我目前使用越来越以下错误“对象变量或与块变量未设置”代码

任何人都可以建议我在下面的代码。

希望对此有清楚的解释。如果不是,请询问更多解释。

感谢

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 
+1

通过固定压痕所以它是格式化代码,请正确格式的代码,并删除双行距:) – samiles

+0

并说这行的错误。 – SJR

+0

嗨,谢谢你的回复。 从这一行设置PPslide = PPpres.Slides(4).Shapes.Paste 我收到错误为“öbject变量或块变量未设置”。在打开Powerpoint的模板 之后,excel工作表的范围是复制,但它不粘贴在特定的幻灯片(如上所述的第4张幻灯片)中。 –

回答

0

尝试改变:

'Set PPslide = PPpres.Slides.Add(5, 33) 
PP.ActiveWindow.View.GotoSlide (4) 
Set PPslide = PPpres.Slides(4).Shapes.Paste '<< CHANGING THIS LINE ONLY 
'Paste to PowerPoint and position 
PPslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile 

要:

'Set PPslide = PPpres.Slides.Add(5, 33) 
PP.ActiveWindow.View.GotoSlide (4) 
Set PPslide = PPpres.Slides(4) 
'Paste to PowerPoint and position 
PPslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile 

而且,按照我的意见,你需要更改的下列最后几行您的代码:

'Make PowerPoint Visible and Active 
PowerPointApp.Visible = True 
PowerPointApp.Activate 
'Clear The Clipboard 
Application.CutCopyMode = Falseenter code here` 
End Sub 

要:

'Make PowerPoint Visible and Active 
PP.Visible = True 
PP.Activate 
'Clear The Clipboard 
Application.CutCopyMode = False 
'enter code here 
End Sub 
+0

非常感谢。..正常工作..但是在下面的行中得到运行时错误424作为“Object Required”代码 'Make PowerPoint Visible and Active PowerPointApp.Visible = True .. –

+0

对于迟到的答复道歉..非常感谢您的快速回复..帮了我很多 –

+0

没问题,很高兴我可以帮助。如果你对答案完全满意,不要忘记标记答案。 – CLR

相关问题