gpt4 book ai didi

vba - 使用 DataObject.PutInClipboard 方法时出现运行时错误

转载 作者:行者123 更新时间:2023-12-04 13:52:58 25 4
gpt4 key购买 nike

我有一个在我的电脑上运行的宏。当其他人运行它时,它会抛出以下异常:

"Run-time error '-2147221036 (800401d4)'
DataObject:PutInClipboard CloseClipboard Failed"

这是我的代码:

Dim buf As String, FSO As Object
Dim CB As New DataObject

Set FSO = CreateObject("Scripting.FileSystemObject")
With FSO.OpenTextFile(sFile, 1)
buf = .ReadAll
buf = Replace(buf, ",", Chr(9))
.Close
End With

With CB
.SetText buf
.PutInClipboard // Here cause the exception.
End With

最佳答案

我遇到了同样的问题。我不知道是什么原因造成的;我的猜测是,如果您的 PC 资源被占用,剪贴板可能不会像您希望的那样快速执行。我的解决方案是将代码放入一个循环中,并在它运行时中断。

Dim buf As String, FSO As Object
Dim CB As New DataObject
dim errnum as long
dim errdesc as string
dim i as long

Set FSO = CreateObject("Scripting.FileSystemObject")
With FSO.OpenTextFile(sFile, 1)
buf = .ReadAll
buf = Replace(buf, ",", Chr(9))
.Close
End With

With CB
.SetText buf

On Error Resume Next
For i=1 to 200
.PutInClipboard
errnum = Err.Number
errdesc = Err.Description
If errnum = 0 Then Exit For
Next i
On Error Goto 0

If errnum > 0 Then
' Do something to handle an epic failure... didn't work even after
' 200 tries.
Err.Raise errnum, errdesc
End If

End With

我必须对 Worksheet.PasteSpecial 做同样的事情。

关于vba - 使用 DataObject.PutInClipboard 方法时出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11554063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com