2013-07-02 23 views
0

我想從Excel中插入數據到PowerPoint表格。到目前爲止,我的代碼完成了這個功能,但是當它與一個真正的PowerPoint文件一起使用時,幻燈片中有很多項目,而我沒有正確處理。我怎樣才能瀏覽幻燈片中的項目列表,並在項目是表格後執行我的代碼?如何檢查表格的PowerPoint幻燈片項目

編輯:Office 2007的/我被要求貼上我的代碼:

Sub AktualisierePowerpointVonExcel() 

Dim AnzahlZeilen As Long 
Dim AnzahlSlides As Long 
Dim App As Object 
Dim CurrSlide As Object 
Dim AktuelleIterationenFuerSlides As Long 
Dim AktuelleIterationenFuerZielZeilen As Long 
Dim z As Long 
Dim SHP As Shape 

On Error GoTo Fehler 

z = 1 

AnzahlZeilen = Range("A65536").End(xlUp).Row 

Set App = CreateObject("PowerPoint.Application") 
App.Visible = msoTrue 
App.Presentations.Open "c:\Users\X\Desktop\1.pptm" 

AnzahlSlides = App.ActivePresentation.Slides.Count 

If (AnzahlZeilen/6) > AnzahlSlides Then 

    MsgBox "Zu wenig Slides für Einträge" & "Anzahl Slides:" & AnzahlSlides & "Anzahl Zeilen:" & AnzahlZeilen & "Benötigte Anzahl An Folien:" & (AnzahlZeilen/6) 

Exit Sub 

Else 



      For AktuelleIterationenFuerSlides = 1 To AnzahlSlides 

      Set CurrSlide = App.ActivePresentation.Slides(AktuelleIterationenFuerSlides) 

       For AktuelleIterationenFuerZielZeilen = 1 To 6 

        For Each SHP In CurrSlide.Shapes 

         If SHP.HasTable Then 

         Worksheets("Tabelle2").Cells(z, 1).Copy 
         SHP.Table.Cell(AktuelleIterationenFuerZielZeilen, 1).Shape.TextFrame.TextRange.Paste 

         Worksheets("Tabelle2").Cells(z, 2).Copy 
         SHP.Table.Cell(AktuelleIterationenFuerZielZeilen, 1).Shape.TextFrame.TextRange.Paste 

         Worksheets("Tabelle2").Cells(z, 3).Copy 
         SHP.Table.Cell(AktuelleIterationenFuerZielZeilen, 1).Shape.TextFrame.TextRange.Paste 

         z = z + 1 

         On Error Resume Next 

         End If 

        Next 

       Next 

      Next 



End If 

Fehler: 
MsgBox "Fehler in Sub Fehler0" & vbCrLf & "Fehlernummer: " & Err.Number & _ 
    vbCrLf & "Fehlerbeschreibung: " & Err.Description 

End Sub 
+0

你可以添加你的代碼嗎? – fvrghl

+0

只是爲了澄清 - 你不知道是否'.Item(1)'是你的桌子,你需要確定嗎?你的幻燈片中只有一張桌子嗎?你需要粘貼或者你只想把價值從Excel的PP表格單元格? –

+0

@KazJaw確切地說,我只需要知道.Item(1)是否是我希望插入我的值的表格。通常只有一張桌子。如果不是,則轉到下一個項目,直到檢查所有項目。 – chrnit

回答

0

這是完整的過程,它允許檢查該滑塊形狀表。你需要循環檢查每個Shape的.Type property。如果你有一個表,那麼...:

Sub Check_if_shape_is_table() 

    Dim CurrSlide As Slide 
    Set CurrSlide = ActivePresentation.Slides(1) 'just for test- change accordingly 

    'your copy code here: 
    Worksheets("Tabelle2").Cells(Z, 1).Copy 

    Dim SHP As Shape 
    For Each SHP In CurrSlide.Shapes 
     If SHP.Type = msoTable Then 

      'change references to your cell accordingly 
      SHP.Table.Cell(AktuelleIterationenFuerZielZeilen, 1).Shape.TextFrame.TextRange.Paste 
     End If 
    Next 

End Sub 

上面的代碼會將值應用於幻燈片中每個表中的單元格。假設只有一張桌子,它會正常工作。

替代解決方案。如果有更多的表,你需要的值添加到最後一個表格(!),你可以做到這一點的方法:

Sub Check_if_shape_is_table_FEW_TABLES() 

    Dim CurrSlide As Slide 
    Set CurrSlide = ActivePresentation.Slides(1) 'just for test change accordingly 

    'your copy code here: 
    Worksheets("Tabelle2").Cells(Z, 1).Copy 

    Dim lastTableSHP As Shape 

    Dim SHP As Shape 
    For Each SHP In CurrSlide.Shapes 
     If SHP.Type = msoTable Then 
      'this will set temp variable of lastTableSHP 
      Set lastTableSHP = SHP 
     End If 
    Next 
    'apply value to the last table in the slide 
    lastTableSHP.Table.Cell(AktuelleIterationenFuerZielZeilen, 1).Shape.TextFrame.TextRange.Paste 

End Sub 
+0

謝謝,我會試試這個 – chrnit

+0

現在你在我的答案中有兩個解決方案! –

+0

你太棒了! :) – chrnit

2

檢查Shape.Type是不可靠的任何更長的時間。 Shape.Type = msoTable如果用戶將表插入到幻燈片中,但是如果他們向內容佔位符添加了表,則類型將有所不同。這更值得信賴:

If Shape.HasTable Then 
    MsgBox "It's a table." 
End If 
+0

我總是忘記... +1這個提示。 –

+0

總是站着,準備好成爲保姆。那是我。 ;-) –