2015-09-23 76 views

回答

5

在另一篇文章中看到了這一點,我用Word VBA對它進行了測試。

'Clearing the Office Clipboard 

    Dim oData As New DataObject 'object to use the clipboard 

    oData.SetText text:=Empty 'Clear 
    oData.PutInClipboard 'take in the clipboard to empty it 

只需複製並粘貼到代碼中,您需要清除剪貼板。

我注意到的另一件事是當我編寫一個程序,比如Excel時,它會一直問我是否要保留數據是剪貼板。解決辦法是使用上述代碼清除剪貼板。如下所示:

'Clearing the Office Clipboard 

    Dim oData As New DataObject 'object to use the clipboard 

    oData.SetText text:=Empty 'Clear 
    oData.PutInClipboard 'take in the clipboard to empty it 


'You can also just remove the Alert Messages from the Excel Program while  
'the code is running 
'Remove alert Messages from the Excel Program when closing 
ExcelProgram.DisplayAlerts = False 

'Quiting the Excel Application 
ExcelProgram.Quit 

我在VBA代碼中使用上述示例從Excel文件導入數據。請參閱here

1

此功能位於庫「Microsoft Forms 2.0 Object Library」中。要鏈接到該庫,請轉至VBA編輯器,然後選擇「工具」,「引用」,如果尚未勾選,請從列表中選取它們。

你可以用一堆WinAPI調用做更多時髦的事情,但我通常更喜歡避免這些,除非絕對必要。

此外,不要忘記DisplayAlerts屬性,它會壓制對話框 - 雖然我不知道它是否總是會產生所需的結果。

+0

感謝你的理解,'DisplayAlerts'屬性非常適用於阻止來自Excel的警報。我認爲,如果要重複使用從Excel導入數據,在每個「循環」結尾處清除剪貼板將是有益的。 –

+0

只需確認;該屬性在Word中也可用。我沒有檢查Word的枚舉值與Excel的屬性的比較。 – NeepNeepNeep

+0

我在Word和Excel中使用了'DisplayAlerts',調試器很開心,所以我相信它會很好。在Word中將使用'Application.DisplayAlerts'並且對於已打開的Active Excel程序它與上面顯示的相同 –

4

說一個簡單的

Application.CutCopyMode = False 

工作您的情況,或者是這個選項不是可行?

+1

謝謝我以前見過,但從未嘗試過。它也適用於我的應用程序。它基本上刪除了CutCopy功能,它會自動刪除剪貼板數據。 –

+0

我喜歡這個解決方案,因爲它不需要添加對象庫的引用。 – ChrisB

0

這是一個適合我的解決方案。這是基於後由扎克Barresse上VBAexpress.com

Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long 
Public Declare Function EmptyClipboard Lib "user32"() As Long 
Public Declare Function CloseClipboard Lib "user32"() As Long 

Public Sub ClearClipboard() 
    OpenClipboard (0&) 
    EmptyClipboard 
    CloseClipboard 
End Sub 

一個這是您的VBA項目,用於ClearClipboard將其清除。