我正在尝试自动将某些展览品从Excel复制到PPT模板,以应对大约500种不同的情况。不幸的是,我遇到了错误,错误地抛出错误462。我已经浏览了几个小时的在线资源,找不到任何有用的信息。我知道常见的错误是后期绑定。但是,我已尝试尽我所能。非常感谢您的帮助。
Dim myBook As Workbook
Set myBook = ThisWorkbook
Dim pptName As String
Dim DestinationPPT As String
Dim myShape As Object
Dim ppt As Object
Dim mySlide As Object
Dim pptA As Object
Set pptA = CreateObject("PowerPoint.Application")
DestinationPPT = myBook.path & "\template.pptx"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'Call Module1.mkdirectories(myBook.path)
Dim inputRange As Excel.Range
Dim c As Excel.Range
Dim worksheetCt As Integer
worksheetCt = 8
myBook.Worksheets("input").Activate
Set inputRange = Evaluate(myBook.Worksheets("input").Range("c4").Validation.Formula1)
For Each c In inputRange
Range("c4").Value = c
Calculate
Set ppt = pptA.Presentations.Open(DestinationPPT)
For worksheetCt = 8 To 39
'On Error Resume Next
myBook.Activate
myBook.Worksheets(worksheetCt).Range("Print_Area").Copy
DoEvents
pptA.Activate
'ppt.Activate
Set mySlide = ppt.Slides(worksheetCt - 5)
'Call Module1.UglyWait
**mySlide.Shapes.PasteSpecial DataType:=2**
'Call Module1.UglyWait
**Set myShape = mySlide.Shapes(mySlide.Shapes.Count)**
粗线是代码失败的地方。
答案 0 :(得分:0)
我发现通过在单独的程序中复制和粘贴所有图表,我已经不再遇到问题。我猜想将幻灯片神奇地传递给另一个程序会导致它有效,并且将复制/粘贴放入循环中多次尝试似乎解决了粘贴问题。
Public Sub PPPasteShape(mySlide As PowerPoint.Slide, pc As ChartObject)
Dim i As Integer
Dim worked As Boolean
i = 0
pc.Copy
worked = False
Do While (i < 5 And worked = False)
Debug.Print "Try: " & CStr(i)
If i > 0 Then
Application.Wait (Now + TimeValue("0:00:01"))
End If
worked = PPTryPasteShape(mySlide)
i = i + 1
Loop
End Sub
Public Function PPTryPasteShape(mySlide As PowerPoint.Slide) As Boolean
On Error GoTo err
mySlide.Shapes.Paste
PPTryPasteShape = True
Exit Function
err:
PPTryPasteShape = False
End Function
Public Sub PPPasteRange(mySlide As PowerPoint.Slide, rng As Range)
Dim i As Integer
Dim worked As Boolean
i = 0
rng.Copy
Do While (i < 5 And worked = False)
Debug.Print "Try: " & CStr(i)
If i > 0 Then
Application.Wait (Now + TimeValue("0:00:01"))
End If
worked = PPTryPasteRange(mySlide)
i = i + 1
Loop
End Sub
Public Function PPTryPasteRange(mySlide As PowerPoint.Slide) As Boolean
On Error GoTo err
mySlide.Shapes.Paste
PPTryPasteRange = True
Exit Function
err:
PPTryPasteRange = False
End Function
答案 1 :(得分:0)
也许声明为
Dim myShape As Powerpoint.Shape
Dim ppt As Powerpoint.Presentation
Dim mySlide As Powerpoint.Slide