Private Function TransferShape(stMT$, ByVal spSp As Shape, ByVal rgSpTarget As Range) As Integer
Dim inAttempts%, inMaxAttempts%, inErrType%
''' Selection here assumes paste to target range is in the Active sheet
rgSpTarget.Select
procRetry: inAttempts = 0: inMaxAttempts = 100
Do: inErrType = 0
''' Yield to OS occasionally
If inAttempts Mod 20 = 0 Then DoEvents
''' With local error handling: Attempt the copy and paste
''' Note: Recording failed to copy as error type 601, and failed to paste as error type 602
''' Though not currently acting any differently based on type
On Error Resume Next: Err.Clear: spSp.Copy
If Err <> 0 Then
inErrType = 601
Else
ActiveSheet.Paste
If Err <> 0 Then inErrType = 602
End If
On Error GoTo 0
''' No error type: Exit Do now (all done)
If inErrType = 0 Then Exit Do
''' Attempt failed: Increment attempts and try again (until Max Attempts reached).
inAttempts = inAttempts + 1
Loop Until inAttempts = inMaxAttempts
''' Failed: Prompt user to keep trying or not
If inErrType Then
If MsgBox(Buttons:=69, Title:=stMT, Prompt:= _
"Attempting to copy and paste a picture failed " & inMaxAttempts & " times." & vbLf & vbLf & _
"Shall we try again?") = vbRetry Then GoTo procRetry
End If
''' DONE: Clear local objects and return Error Type (if any)
Set spSp = Nothing: Set rgSpTarget = Nothing: TransferShape = inErrType
End Function
1条答案
按热度按时间2vuwiymt1#
也遇到了同样的问题。到目前为止,没有明显的节奏或原因。即随机选择和复制形状的代码失败,并报告错误消息。
能够缩小一些方面:
a)似乎只发生在windows 10与office 365(其他系统,没有问题)
B)如果用户重新启动他们的系统并再次运行相同的代码,则一切正常。
编辑:经过进一步的测试和研究,得出的结论是Excel中的完全随机故障。
GWteB在Mr Excel也得出了同样的结论(由于广告饱和,链接被删除)。那里提出的观点解决了部分问题。然而,他们只经历了粘贴失败。而我看到复制和粘贴都失败了。
我已经实现了一个函数(扩展了GWteB的思想):
执行上述操作后,所有形状复制和粘贴失败均停止。
有趣的是,也没有看到最大尝试达到。但进一步的测试是另一天。