2017-10-11 7 views
1

J'ai cette diapositives de présentation qui a déjà son contenu. Mon objectif est maintenant de coller de nouvelles choses d'Excel vers les mêmes diapositives, au lieu d'ajouter de nouvelles diapositives au fichier PowerPoint. La langue que j'utilise est VBA.Pour coller la plage de cellules d'Excel à Powerpoint

Voici mon code:

Sub CreatePowerPoint() 
'------------------------------------------- 
'ADD A REFERENCE TO THE MICROSOFT POWERPOINT LIBRARY BY: 
    '1. Go to Tools in the VBA menu 
    '2. Click on Reference 
    '3. Scroll down to Microsoft PowerPoint X.0 Object Library, check the box, and press Okay 
'------------------------------------------- 
'FIRST WE DECLARE THE VARIABLES WE WILL BE USING 
    Dim newPowerPoint As PowerPoint.Application 
    Dim activeSlide As PowerPoint.Slide 
    'Dim tbl As ListObject 
    Dim rng As Range, row As Long 
    Dim sht As Worksheet 
    row = 1 
'------------------------------------------- 
'LOOK FOR EXISTING INSTANCE 
    On Error Resume Next 
    Set newPowerPoint = GetObject(, "PowerPoint.Application") 
    On Error GoTo 0 
'------------------------------------------- 
'LET'S CREATE A NEW POWERPOINT 
    If newPowerPoint Is Nothing Then 
     Set newPowerPoint = New PowerPoint.Application 
    End If 
'------------------------------------------- 
'MAKE A PRESENTATION IN POWERPOINT 
    If newPowerPoint.Presentations.Count = 0 Then 
     newPowerPoint.Presentations.Add 
    End If 
'------------------------------------------- 
'SHOW THE POWERPOINT 
    newPowerPoint.Visible = True 
'------------------------------------------- 
'LOOP THROUGH EACH RANGE DATA SET IN THE EXCEL WORKBOOK AND PASTE THEM INTO THE POWERPOINT 
    For Each sht In ThisWorkbook.Worksheets 
     'For Each tbl In sht.ListObjects 
     LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).row 
     Do Until row >= LastRow 
      For I = row To LastRow 
       If Cells(I, 1) = "|" Then 
        LastCol = ActiveSheet.Cells(row, Application.Columns.Count).End(xlToLeft).Column 
        Set rng = Range(Cells(row, 1), Cells(I - 1, LastCol)) 

'Note: xlUp, xlToLeft = Returns a Range object that represents the cell at the end of the region that contains the source range. 
'Equivalent to pressing END+UP ARROW, END+DOWN ARROW, END+LEFT ARROW, or END+RIGHT ARROW. Read-only Range object. 
'------------------------------------------- 
'ADD A NEW SLIDE WHERE WE WILL PASTE THE TABLE 
        newPowerPoint.ActivePresentation.Slides.Add newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutText 
        newPowerPoint.ActiveWindow.View.GotoSlide newPowerPoint.ActivePresentation.Slides.Count 
        Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count) 
'Note: ppLayoutText = the layout of the slide 
'------------------------------------------- 
'COPY THE TABLE AND PASTE IT INTO THE POWERPOINT AS DEFAULT (EDITABLE FORM IN PPT) 
        'tbl.Range.Copy 
        rng.Copy 
        activeSlide.Shapes.PasteSpecial(DataType:=ppPasteDefault).Select 
'Note:ppPasteEnhancedMetafile/ppPasteOLEObject = if want to edit in Excel form 
'------------------------------------------- 
'ADJUST THE POSITIONING OF THE TABLE ON POWERPOINT SLIDE 
        With newPowerPoint.ActiveWindow.Selection.ShapeRange 
         .Left = 20 
         .Top = 125 
         .Width = 675 
        End With 
        activeSlide.Shapes(2).Width = 200 
        activeSlide.Shapes(2).Left = 505 
        row = I 
        Exit For 
       End If 
     'Next tbl 
      Next I 
      row = row + 1 
     Loop 

    Next sht 
'------------------------------------------- 
'CLEANUP 
    Set activeSlide = Nothing 
    Set newPowerPoint = Nothing 
End Sub 

Au lieu d'ajouter de nouvelles diapositives, je voudrais le coller dans un diapositives PowerPoint actif qui a déjà un contenu en elle. Quelqu'un peut-il m'aider? Merci d'avance.

Répondre

0

Vous devez référencer explicitement la diapositive. Je referais explicitement la présentation aussi. les diapositives (1) fait référence à la diapositive numéro 1.

'COPY THE TABLE AND PASTE IT INTO THE POWERPOINT AS DEFAULT (EDITABLE FORM IN PPT) 
        'tbl.Range.Copy 
        rng.Copy 
        Presentation ("PPT file name.pptx").Slides(1).Shapes.PasteSpecial(DataType:=ppPasteDefault).Select 
+0

Salut Mooseman, la question est table spécifique doit être collé à la diapositive spécifique. Comment puis-je référencer une table spécifique à une diapositive spécifique? Y a-t-il un moyen? –

+0

Eh bien, je viens de vous montrer comment définir une diapositive spécifique, il vous suffit de définir une table spécifique. Classeur ("nom de fichier Excel" .Sheets ("nom de la feuille ou numéro d'index) .table ('nom de la table"). Copy Quelque chose comme ça. – mooseman