2017-07-30 14 views
0

有誰知道如何在Excel中使用「My.Computer.Clipboard」對象。我已經嘗試了幾乎所有的參考,通過工具 - >參考。我想用這樣的代碼Excel中的「My.Computer.Clipboard」對象

If My.Computer.Clipboard.ContainsImage() Then 
     Dim grabpicture As System.Drawing.Image 
     grabpicture = My.Computer.Clipboard.GetImage() 
     picturebox1.Image = grabpicture 
End If 

https://docs.microsoft.com/en-us/dotnet/visual-basic/developing-apps/programming/computer-resources/storing-data-to-and-reading-from-the-clipboard

感謝

+1

您似乎正在查看vb.net代碼,但您可能希望查看MSForms.DataObjects以使其在Excel中工作 - 請嘗試http://excel-macro.tutorialhorizo​​n.com/vba-excel-putting-文本在Windows剪貼板/ – jamheadart

+0

謝謝@jamheadart –

回答

0

在模塊中,把這個代碼(或相同的模塊比你的測試代碼):

Option Explicit 

#If Win64 Then 
    Public Declare PtrSafe Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long 
#Else 
    Public Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long 
#End If 

比,你可以通過下面的代碼來做你的測試。

If IsClipboardFormatAvailable(2) <> 0 Or IsClipboardFormatAvailable(14) <> 0 Then ... 

注意:14代表圖元文件,2代表位圖。

注2:這適用於64位和32位Excel。

注3:爲尋找如果文本存在:if IsClipboardFormatAvailable(1)<>0 then ...

爲了得到從剪貼板中的數據(圖像)的圖像控制(在一個窗體,我猜),使用Me.Image1.Picture = PastePicture,您可以通過谷歌找到它的代碼 - 「ModPastePicture」也是,因爲這不是適用於vba的方法。這比將圖片保存到磁盤並將其重新加載到控制圖像中要快得多。