2015-10-22 153 views
0

我想使用VBA(Excel和PowerPoint 2013)將幾個圖表複製到PowerPoint。只要我不試圖破壞Excel和PowerPoint之間的圖形連接,我的宏就可以正常工作 - 我絕對需要這樣做。將Excel圖表複製/粘貼到PowerPoint並斷開鏈接

我在Google上查找過,發現有人建議使用.Breaklink方法:只要在我的工作表上沒有多於一個圖表,它工作得非常好,實際上會中斷鏈接。如果至少有兩個圖形,它將正確複製第一個圖形,然後在第二個圖形上工作時拋出「MS PowerPoint已停止工作」消息。

我該如何繼續?

我試圖在.Chart.ChartData和.Shape對象上應用.BreakLink方法無濟於事。

Sub WhyIsThisWrong() 
    Application.ScreenUpdating = False 
    Dim aPPT As PowerPoint.Application 
    Dim oSld As PowerPoint.Slide 
    Dim oShp As PowerPoint.Shape 
    Dim oCh As ChartObject 

     Set aPPT = New PowerPoint.Application 
     aPPT.Presentations.Add 
     aPPT.Visible = True 

     For Each oCh In ActiveSheet.ChartObjects 
     oCh.Activate 
     ActiveChart.ChartArea.Copy 

     aPPT.ActivePresentation.Slides.Add aPPT.ActivePresentation.Slides.Count + 1, ppLayoutText 
     Set oSld = aPPT.ActivePresentation.Slides(aPPT.ActivePresentation.Slides.Count) 

     oSld.Shapes.PasteSpecial(DataType:=ppPasteDefault).Select 

     'Something is wrong here 
     With oSld.Shapes(3) 
      If .Chart.ChartData.IsLinked Then 
      '.Chart.ChartData.BreakLink 
      .LinkFormat.BreakLink 
      End If 
     End With 

     Next oCh 

    Set oSld = Nothing 
    Set aPPT = Nothing 
    Application.ScreenUpdating = True 
    End Sub 

回答

1

這可能不是您確切的答案 - 它將圖表作爲圖片粘貼到Powerpoint中。
注:沒有提及需要設置爲PP,並應工作在至少XL & PP 2007,2010 & 2013年

我已經更新了代碼,具有兩個粘貼爲圖片粘貼的圖表,斷開鏈接。希望它不是那種在我的機器上工作的情況之一..

Public Sub UpdatePowerPoint() 

    Dim oPPT As Object 
    Dim oPresentation As Object 
    Dim cht As Chart 

    Set oPPT = CreatePPT 
    Set oPresentation = oPPT.presentations.Open(_ 
     "<Full Path to your presentation>") 

    oPPT.ActiveWindow.viewtype = 1 '1 = ppViewSlide 

    ''''''''''''''''''''''''' 
    'Copy Chart to Slide 2. ' 
    ''''''''''''''''''''''''' 
    oPresentation.Windows(1).View.goToSlide 2 
    With oPresentation.Slides(2) 
     .Select 
     Set cht = ThisWorkbook.Worksheets("MySheetWithAChart").ChartObjects("MyChart").Chart 

     '''''''''''''''''''''''''' 
     'Paste Chart as picture. ' 
     '''''''''''''''''''''''''' 
'  cht.CopyPicture Appearance:=xlScreen, Format:=xlPicture, Size:=xlScreen 
'  .Shapes.Paste.Select 

     ''''''''''''''''''''''''''''''''' 
     'Paste as Chart and break link. ' 
     ''''''''''''''''''''''''''''''''' 
     cht.ChartArea.Copy 
     .Shapes.Paste.Select 
     With .Shapes("MyChart") 
      .LinkFormat.BreakLink 
     End With 

     oPresentation.Windows(1).Selection.ShapeRange.Left = 150 
     oPresentation.Windows(1).Selection.ShapeRange.Top = 90 
    End With 

End Sub 

    '---------------------------------------------------------------------------------- 
    ' Procedure : CreatePPT 
    ' Date  : 02/10/2014 
    ' Purpose : Creates an instance of Powerpoint and passes the reference back. 
    '----------------------------------------------------------------------------------- 
    Public Function CreatePPT(Optional bVisible As Boolean = True) As Object 

     Dim oTmpPPT As Object 

     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
     'Defer error trapping in case PowerPoint is not running. ' 
     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
     On Error Resume Next 
     Set oTmpPPT = GetObject(, "PowerPoint.Application") 

     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
     'If an error occurs then create an instance of PowerPoint. ' 
     'Reinstate error handling.         ' 
     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
     If Err.Number <> 0 Then 
      Err.Clear 
      On Error GoTo ERROR_HANDLER 
      Set oTmpPPT = CreateObject("PowerPoint.Application") 
     End If 

     oTmpPPT.Visible = bVisible 
     Set CreatePPT = oTmpPPT 

     On Error GoTo 0 
     Exit Function 

    ERROR_HANDLER: 
     Select Case Err.Number 

      Case Else 
       MsgBox "Error " & Err.Number & vbCr & _ 
        " (" & Err.Description & ") in procedure CreatePPT." 
       Err.Clear 
     End Select 

    End Function 
+0

謝謝。作爲圖像粘貼會有點解決我的問題,但它看起來很醜,圖像幾乎不會調整大小。 此外,我試圖將代碼更改爲使用不同的粘貼方法,但是當試圖在對象「演示文稿」上應用「打開」方法時,代碼會提供中斷。 如果有人瞭解PowerPoint爲什麼會退出我的代碼......也許我使用的引用有問題:我應該選擇Microsoft Object Libray,MS PowerPoint,兩者......? – jodoox

+0

確實如此 - 圖像確實會丟失一些定義,您需要在Excel中將它們設置爲正確的大小。不知道爲什麼它在Open方法上下降了 - 是完整路徑是否正確? –

+0

我的不好,我在路上有一個錯字。我仍然想保留圖表格式:沒有關於.BreakLink方法有什麼問題的線索? – jodoox