2012-07-19 589 views
3

我有一個運行在我的PC上的宏。當別人運行它,它會引發以下異常:使用DataObject.PutInClipboard時出現運行時錯誤方法

"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 
+0

您的代碼適用於我。正如我所料,我想。當我使用API​​調用來訪問剪貼板,然後嘗試使用'PutInClipboard'方法時,我遇到了類似的錯誤。 – mkingston 2012-07-19 05:03:56

+0

@mkingston謝謝。趕上麻煩真的很難! – shenhengbin 2012-07-19 05:20:26

+0

是的,我可以相信它。祝你好運,對不起,我忍不住了。 – mkingston 2012-07-19 05:25:36

回答

1

我有同樣的問題。我不知道是什麼原因造成的;我的猜測是,如果您的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做同樣的事情。

-1

我也遇到了同樣的錯誤在Windows 10(64位),Word 2003中,當我使用了錯誤已經消失:

.Clear 

即清除了數據對象,之前:

.SetText 

編輯:也適用於Windows 10(32位),Word 2013

相關問題