【问题标题】:copy from VBA excel to powerpoint从 VBA excel 复制到 powerpoint
【发布时间】: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

标签: excel vba


【解决方案1】:

尝试改变:

'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

【讨论】:

  • 非常感谢.. 工作正常.. 但是在下面的代码行中将运行时错误 424 设置为“需要对象”'Make PowerPoint Visible and Active PowerPointApp.Visible = True..
  • 抱歉回复晚了..非常感谢您的快速回复..帮了我很多
  • 没问题,很高兴能帮上忙。如果您对答案完全满意,请不要忘记将其标记为已回答。
猜你喜欢
  • 2016-11-02
  • 2012-10-16
  • 2018-01-26
  • 2015-02-06
  • 1970-01-01
  • 2015-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多