Excel范围到PowerPoint - 粘贴问题

时间:2013-12-11 16:09:05

标签: excel vba format powerpoint paste

在将范围从Excel粘贴到PowerPoint时,我遇到了一些问题。我希望将其保留为Keepsource格式:

Function copyToPPT()

'Create an instance of PowerPoint.
Set pptApp = CreateObject("PowerPoint.Application")
' Create a PowerPoint presentation.
nomeppt = ThisWorkbook.Path + "\" + "SR-1871_R1 - ID-033 - Bi-Weekly LATAM QC Communication Meeting - data_Blank.pptx"

With pptApp
    Let .Visible = True
    Let .WindowState = 3
    Set Pres1 = pptApp.Presentations.Open(nomeppt)
End With


i = 8
While i <= 14
    slide = "Slide " & i & " Final"
    Workbooks("Results.xlsx").Activate
    Worksheets(slide).Activate
    Worksheets(slide).Range("A1").Select
    Worksheets(slide).Range(Selection, Selection.End(xlDown)).Select
    Worksheets(slide).Range(Selection, Selection.End(xlToRight)).Select 'Selecionando os registros - Simulando ctrl + shift baixo/direta
    Selection.Copy
    pptApp.ActiveWindow.View.GotoSlide Index:=i
    'pptApp.ActivePresentation.Slides(i).Shapes.PasteSpecial DataType:=7 - NOT THE FORMAT I WANT
    i = i + 1
    pptApp.CommandBars.ExecuteMso ("PasteSourceFormatting") 'freeze the powerpoint when pasting...
    pptApp.CommandBars.ReleaseFocus

Wend

End Function

1 个答案:

答案 0 :(得分:1)

试试这个

pptApp.ActiveWindow.View.PasteSpecial DataType:=ppPasteDefault

这给出与

相同的结果
pptApp.CommandBars.ExecuteMso ("PasteSourceFormatting")

ppPasteDefault的值为0所以你可以把

Const ppPasteDefault as Integer = 0

位于代码顶部或使用

pptApp.ActiveWindow.View.PasteSpecial DataType:=0

编辑(评论后续跟踪)

我已经更改了你的代码。使用此并告诉我您是否收到任何错误。这不会使用.Activate/.Select INTERESTING READ

试试这个

Sub copyToPPT()
    Dim lRow As Long, lCol As Long
    Dim LastCol As String
    Dim rng As Range

    'Create an instance of PowerPoint.
    Set pptApp = CreateObject("PowerPoint.Application")
    ' Create a PowerPoint presentation.
    nomeppt = ThisWorkbook.Path & "\" & _
    "SR-1871_R1 - ID-033 - Bi-Weekly LATAM QC Communication Meeting - data_Blank.pptx"

    With pptApp
        .Visible = True
        .WindowState = 3
        Set Pres1 = pptApp.Presentations.Open(nomeppt)
    End With

    i = 8

    While i <= 14
        slide = "Slide " & i & " Final"
        With Workbooks("Results.xlsx").Worksheets(slide)
            lRow = .Range("A" & .Rows.Count).End(xlUp).Row
            lCol = .Cells(1, Columns.Count).End(xlToLeft).Column

            LastCol = Split(.Cells(, lCol).Address, "$")(1)

            Set rng = .Range("A1:" & LastCol & lRow)
        End With

        pptApp.ActiveWindow.View.GotoSlide Index:=i

        rng.Copy

        DoEvents

        pptApp.ActiveWindow.Panes(2).Activate

        pptApp.CommandBars.ExecuteMso ("PasteSourceFormatting")

        Wait 3

        Application.CutCopyMode = False

        i = i + 1
    Wend
End Sub

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub