2015-05-14 44 views
0

我試圖自動創建與Excel中創建的PowerPoint的創建。我的問題是在表格粘貼成powerpoint後,它不會被視爲「被選中」。我收到一個錯誤,通常表示類似'沒有選擇對象'或'對象'選擇'失敗的'方法'ShareRange'「。如果我使用F8慢慢瀏覽代碼,那麼大多數情況下,它運行時不起作用。Excel到Powerpoint(但與表不是圖片)

在任何情況下,我已經嘗試使用表的名稱(當表中的副本獲得名稱'表1'想象)),我試着讓它等待幾秒鐘和其他一些東西。我嘗試了不同類型的粘貼到文件中,但是我需要將它保存爲一張表,而不是一張圖片(如果我只需要一張圖片,我的代碼就可以工作)。 我的問題(我認爲)是它粘貼,並沒有立即選擇。

我修改了一下,只是顯示錯誤的位置,我也一樣地粘貼,移動,大小,一遍又一遍。我真的希望這是一條線 - sma我的臉,因爲它明顯的修復...

Dim pp As PowerPoint.Application 
    Dim PPPres As PowerPoint.Presentation 
    Dim PPSlide As PowerPoint.Slide 
    Dim Slide1Title As Excel.Range 
    'Opening a blank, normally I have it open a template 
    Set pp = New PowerPoint.Application 
    Set PPPres = pp.Presentations.Add 
    Set PPSlide = PPPres.Slides.Add(1, 12) 
    Set PPSlide = PPPres.Slides.Add(2, 12) 
    Set PPSlide = PPPres.Slides.Add(3, 12) 
    pp.Visible = True 


    'Paste as text/table 
    'Title 1 
    PPPres.Slides(2).Select 
    Set Slide1Title = Sheets("presentation").Range("B2:G3") 
    Slide1Title.Copy 
    PPPres.Application.CommandBars.ExecuteMso ("PasteSourceFormatting") 
    'Here-ish is the error, after pasting in I can't seem to select it 
    pp.ActiveWindow.Selection.ShapeRange.Top = 10 
    pp.ActiveWindow.Selection.ShapeRange.Left = 75 

回答

0

常見問題。最近粘貼的形狀將永遠是「上面」所以你可以使用這樣的事情來得到它:

PPPres.Slides(2).Shapes(PPPres.Slides(2).Shapes.Count) 

注意,通過選擇形狀和幻燈片,你打開自己怪異的錯誤,並減緩你的代碼由一個數量級。

Dim oSh as Shape 
With PPPres.Slides(2) 
    ' Do the paste, then get a reference to the pasted shape: 
    Set oSh = PPPres.Slides(2).Shapes(PPPres.Slides(2).Shapes.Count) 
    With oSh 
    ' set .Top, .Left etc 
    End With ' the shape 
End With ' the slide 
+0

當我運行它具有相同的老錯誤,但項,而不是形狀代碼...然後當我通過代碼F8,我得到形狀(未知成員):整數超出範圍。 0不在1到0的有效範圍內。 – Tetsuothecat

+0

GOT IT(我已經成功刪除了選定的幻燈片項目...我認爲用PPPres.Slides(2)也會選擇幻燈片以及...或粘貼它在正確的位置......錯誤的......它粘貼在任何一個被選中的位置上......即使它說'用幻燈片2粘貼'但非常感謝!我錯過了一些細微差別,我之前看過這個代碼,但是我一直把它放在錯誤的地方,再次感謝!! – Tetsuothecat

+0

很高興它的工作......建議:通過發佈最終代碼的答案來付款,或者編輯已有的東西並注意它現在可以工作,然後其他人就可以從你學到的東西中受益 –

0

臨時解決方法,在不使用F8

Set Slide1Title = Sheets("presentation").Range("B2:G3") 
Slide1Title.Copy 
PPPres.Slides(2).Select 
With PPPres.Slides(2) 
PPPres.Application.CommandBars.ExecuteMso ("PasteSourceFormatting") 
MsgBox ("1") 
Set oSh = PPPres.Slides(2).Shapes(PPPres.Slides(2).Shapes.Count) 

    With oSh 
pp.ActiveWindow.Selection.ShapeRange.Top = 10 
pp.ActiveWindow.Selection.ShapeRange.Left = 75 
    End With ' the shape 
End With ' the slide 
相關問題