【问题标题】:VBA Excel --> PWP - Blank when copyVBA Excel --> PWP - 复制时为空白
【发布时间】:2017-07-31 13:58:51
【问题描述】:

我的宏有点问题。我知道这不是完美的,但至少它有效。

唯一的问题是,当我一步一步进行时,一切都很顺利,但是当我运行它时,所有新幻灯片都是空白的。

您知道如何改进吗?

Sub paste_toPPT()

Dim PowerPointApp As Object
Dim pptApp As Object
Dim pptPres As Object
Dim myRange As Excel.Range
Dim path As String
Dim DestinationPPT As String
Dim saveName As String
Dim image As Object
Dim IDe As String
Dim count As Integer

'Create an Instance of PowerPoint
On Error Resume Next
'Is PowerPoint already opened?
Set pptApp = GetObject(Class:="PowerPoint.Application")
'Clear the error between errors
Err.Clear

'If PowerPoint is not already open then open PowerPoint
If pptApp Is Nothing Then Set pptApp = CreateObject(Class:="PowerPoint.Application")
'Handle if the PowerPoint Application is not found
If Err.Number = 429 Then
    MsgBox "PowerPoint could not be found, aborting."
    Exit Sub
End If
On Error GoTo 0

'Open template
DestinationPPT = "C:\Users\user\Desktop\ID Card\Kpi ID.pptx"
Set pptPres = pptApp.Presentations.Open(DestinationPPT)

Windows("KPI List - P2P KPI.xlsm").Activate
count = WorksheetFunction.CountA(Sheets("KPI List").Range("E:E")) - 1

For i = 8 To count
    Worksheets("KPI List").Select
    'ThisWorkbook.Sheets("KPI List").Select
    IDe = Worksheets("KPI List").Range(Cells(i, 5), Cells(i, 5))
    ThisWorkbook.Sheets("ID").Range("F4:F4") = IDe
    'Set the range to copy
    Windows("KPI List - P2P KPI.xlsm").Activate
    Worksheets("ID").Select
    Worksheets("ID").Shapes.Range(Array("Group 57")).Select
    Selection.Copy
    'Add slide & Paste data

    pptPres.Windows(1).Activate
    Set mySlide = pptPres.Slides.Add(1, 12)
    mySlide.Select
    pptApp.CommandBars.ExecuteMso ("PasteSourceFormatting")
Next i

pptPres.SaveAs DestinationPPT

End Sub   

【问题讨论】:

    标签: vba excel powerpoint


    【解决方案1】:

    试试下面的代码,代码里面的解释为cmets:

    Sub paste_toPPT()
    
    Dim pptApp As Object
    Dim pptPres As Object
    Dim myRange As Excel.Range
    Dim path As String
    Dim DestinationPPT As String
    Dim saveName As String
    Dim image As Object
    Dim IDe As String
    Dim count As Integer
    
    ' added 2 worksheet objects
    Dim wsKPI As Worksheet
    Dim wsID As Worksheet
    
    'Create an Instance of PowerPoint
    On Error Resume Next
    'Is PowerPoint already opened?
    Set pptApp = GetObject(, "PowerPoint.Application")
    'Clear the error between errors
    Err.Clear
    
    'If PowerPoint is not already open then open PowerPoint
    If pptApp Is Nothing Then Set pptApp = CreateObject("PowerPoint.Application")
    'Handle if the PowerPoint Application is not found
    If Err.Number = 429 Then
        MsgBox "PowerPoint could not be found, aborting."
        Exit Sub
    End If
    On Error GoTo 0
    
    'Open template
    DestinationPPT = "C:\Users\user\Desktop\ID Card\Kpi ID.pptx"
    Set pptPres = pptApp.Presentations.Open(DestinationPPT)
    
    ' no need to Activate the workbook first, just set the worksheet objects
    Set wsKPI = Workbooks("KPI List - P2P KPI.xlsm").Sheets("KPI List")
    Set wsID = Workbooks("KPI List - P2P KPI.xlsm").Sheets("ID")
    
    count = WorksheetFunction.CountA(ws.Range("E:E")) - 1
    
    For i = 8 To count
        IDe = wsKPI.Range(wsKPI.Cells(i, 5), wsKPI.Cells(i, 5))
        wsID.Range("F4:F4") = IDe
    
        ' first add the slide , later do the copy>>paste as close as can be
        Set mySlide = pptPres.Slides.Add(1, 12)
    
        ' Set the range to copy (no need to Select first)
        wsID.Shapes.Range(Array("Group 57")).Copy
    
        mySlide.Select
        pptApp.CommandBars.ExecuteMso ("PasteSourceFormatting")
    Next i
    
    pptPres.Save
    
    End Sub
    

    【讨论】:

    • count = WorksheetFunction.CountA(ws.Range("E:E")) - 1 应该是 count = WorksheetFunction.CountA(wsKPI.Range("E:E")) - 1 我认为
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    相关资源
    最近更新 更多