使用VBA将Excel数据复制到精确的PowerPoint幻灯片单元格

时间:2018-05-16 10:10:08

标签: excel vba excel-vba copy powerpoint

所以我有一个工作宏将Excel行复制为图片,并将每张图片粘贴到新的PowerPoint幻灯片中。

所以我现在的工作是从一个精确的单元格中获取单个数据(例如A1,D1,H1,X1)并将其粘贴到预定义的PowerPoint幻灯片布局中。因此每个单元格都会进入幻灯片布局中的相应位置。我认为只需要进行一些修改,但我完全不知道该怎么做。我对VBA非常陌生,所以感谢所有的帮助。

感谢您的时间,祝您有个美好的一天! :)

Sub CopyRangeToPresentation()
'Variables
  Dim PP As PowerPoint.Application
  Dim PPpres As PowerPoint.Presentation
  Dim PPslide As PowerPoint.Slide
  Dim SlideTitle As String
  Dim lRow As Long
  Dim i As Integer
'Fider
  lRow = Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row
'New presentation
Set PP = New PowerPoint.Application
  Set PPpres = PP.Presentations.Add
  Set PP = GetObject(, "PowerPoint.Application")
  PP.Visible = 1

For i = 1 To lRow
'New slide
      Set PPslide = PPpres.Slides.Add(i, ppLayoutBlank)
      PP.ActiveWindow.ViewType = ppViewSlide
      PPpres.PageSetup.SlideSize = ppSlideSizeOnScreen
      PP.ActiveWindow.WindowState = ppWindowMaximized
      PPslide.Select
'Copy
      Sheets("dataflows").Range(Cells(i, 1), Cells(i, 24)).CopyPicture _
      Appearance:=xlScreen, Format:=xlPicture
'Paste
      PPslide.Shapes.Paste.Select
      PP.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
      PP.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
'Title
Next i

'Memory
      PP.Activate
      Set PPslide = Nothing
      Set PPpres = Nothing
      Set PP = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

您可以在希望单元格的位置创建带有文本框的自定义布局。文本框内的文本可用于识别它们(例如“mybox1”)。您可以谷歌如何从自定义布局添加新幻灯片。然后搜索形状并将单元格粘贴到相同的位置。像这样:

'Paste
  For Each PPshape In PPslide.Shapes
    If PPshape.HasTextFrame Then
        If PPshape.TextFrame.HasText Then
            If PPshape.TextFrame.TextRange.Text = "mybox1" Then
                PPslide.Shapes.Paste.Select
                PP.ActiveWindow.Selection.ShapeRange.Left = PPshape.Left
                PP.ActiveWindow.Selection.ShapeRange.Top = PPshape.Top
                PPshape.Delete
            End If
        End If
    End If
Next PPshape