安全使用剪切板,不报错。
Option Explicit
Private Declare Sub Sleep Lib "kernel32.dll" Alias "Sleep" (ByVal dwMilliseconds As Long)
Private Const CF_BITMAP = 2
' 避免 Clipboard.SetData 出错
Private Function f_SafeSetClipboardPicture(ByVal lngResPic As Long) As Boolean
Dim iRetry%
Dim blnSuccess As Boolean
' 最多重试3次
For iRetry = 1 To 3
On Error Resume Next
Clipboard.Clear ' 清空剪贴板(先尝试清空,避免占用)
' 设置图片到剪贴板
Clipboard.SetData LoadResPicture(lngResPic, 0)
' 检查是否成功
If Err.Number = 0 Then
blnSuccess = True
Exit For
Else
' 等待一小段时间后重试
If iRetry < 3 Then Sleep 100
Err.Clear
End If
Next iRetry
On Error GoTo 0
f_SafeSetClipboardPicture = blnSuccess
End Function