簡體   English   中英

將圖表從Excel復制到PowerPoint並使用VBA保持源格式

[英]Copying a chart from Excel to PowerPoint and keeping the Source Formatting using VBA

我正在遍歷excel工作簿的工作表,並嘗試繪制一些圖表,然后將這些圖表粘貼到PowerPoint演示文稿中。

我的偽代碼看起來像這樣

Dim counter As Integer
counter = 2

for each ws in ActiveWorkbook.Worksheets
   Dim myChart2 As Chart
   Set myChart2 = ws.Shapes.AddChart.Chart
   'DO chart stuff

   Set ppSlide = ppPres.Slides.Add(counter, ppLayoutTitleOnly)
      With ppSlide.Shapes
        If Not .HasTitle Then
            .AddTitle.TextFrame.TextRange.Text = "Title"
        End If
        ppSlide.Shapes.Title.TextFrame.TextRange.Text = ws.Name

         myChart2.ChartArea.Copy

         ppApp.CommandBars.ExecuteMso ("PasteExcelChartSourceFormatting")

      End With
    counter = counter + 1
    ws.ChartObjects.Delete


Next ws

不幸的是,所有圖表都轉儲在第一張幻燈片上,而不是每張幻燈片上的每個圖表。 誰能幫我糾正這個問題?

嘗試這個:

Dim PPSlide As PowerPoint.Slide

for each ws in ActiveWorkbook.Worksheets
   Dim myChart2 As Chart
   Set myChart2 = ws.Shapes.AddChart.Chart
   'DO chart stuff

   Set ppSlide = ppPres.Slides.Add(counter, ppLayoutTitleOnly)
      With ppSlide.Shapes
        If Not .HasTitle Then
            .AddTitle.TextFrame.TextRange.Text = "Title"
        End If
        ppSlide.Shapes.Title.TextFrame.TextRange.Text = ws.Name

        'Add slide to presentation
        PPApp.ActivePresentation.Slides.Add _
            PPApp.ActivePresentation.Slides.Count + 1, ppLayoutTitleOnly
        PPApp.ActiveWindow.View.GotoSlide _
            PPApp.ActivePresentation.Slides.Count
        Set PPSlide = PPApp.ActivePresentation.Slides( _
            PPApp.ActivePresentation.Slides.Count)

       ppApp.CommandBars.ExecuteMso ("PasteExcelChartSourceFormatting")
       ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM