3

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

"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
4

2 回答 2

2

我有同样的问题。我不知道是什么原因造成的;我的猜测是,如果您的 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.

于 2015-06-15T21:09:31.950 回答
0

我在 Windows 10(64 位)、Word 2003 中也遇到过同样的错误。我用过时错误消失了:

.Clear

即已经清除了DataObject,就在之前:

.SetText

编辑:也适用于 Windows 10(32 位)、Word 2013

于 2016-05-14T04:55:31.527 回答