Excel-vba:仅从剪贴板粘贴图像+允许以后重复使用 [英] Excel-vba: Pasting Image only from Clipboard + allowing for re-use later

查看:140
本文介绍了Excel-vba:仅从剪贴板粘贴图像+允许以后重复使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许用户在excel中执行以下操作.这可能使用宏来实现.

I would like to do allow the user to do following in excel. This would probably be achieved using macros.

  1. 用户像截图工具一样拍摄屏幕截图
  2. 代码检查剪贴板中是否包含图像(仅在剪贴板中包含图像时才运行的代码)
  3. 然后代码将剪贴板中的图像粘贴到指定区域(例如单元格J55).
  4. 同时,我想给粘贴的图像一个ID(例如,imgSource1),以便用户以后可以在另一张纸上重复使用此图像(例如,在纸2上,调用imgSource1并将其粘贴)在那里)

到目前为止,我已经了解了:学习了如何在上述位置将某些内容粘贴到excel中.我无法找到工作代码来检查剪贴板是否保存有图像.现在,我需要弄清楚如何仅粘贴图像(如何在粘贴之前检查剪贴板是否仅保留图像).以下代码似乎不适用于我.

I've gotten thus far: learnt how to paste something into excel at the said location. I haven't been able to find a working code for checking if the clipboard is holding an image or not. Now I need to figure out how to paste only image (how to check clipboard is holding only image before pasting). The following code doesn't seem to work for me.

Sub btn_addImg1()
If (Clipboard.GetImage() != null)
    Sheet1.Paste Destination:=Range("J55"), Link:=False
Else
    'do nothing
End If

" If(Clipboard.GetImage()" 行是红色的,它告诉我它在"!= "处需要一个)".

The "If (Clipboard.GetImage()" line is red, and it's telling me it needs a ")" at "!=".

请注意,对于我的情况,将图像保存在本地驱动器上不是可行的解决方案.必须要从剪贴板粘贴.

Note that saving an image on a local drive is not a feasible solution for my situation. It has to be pasted from the clipboard.

谢谢!

推荐答案

我想出了一些解决方法,但是需要测试.

I figured out some workaround, but it need testing.

顺便说一句,您需要转到工具->参考-> Microsoft Forms 2.0 Oject Librarty,以使 MSForms 正常工作.

Btw, you need to go Tools -> References -> Microsoft Forms 2.0 Oject Librarty to make MSForms working.

    Sub btn_addImg1()
    Dim DataObj As New MSForms.DataObject
    DataObj.GetFromClipboard
    On Error GoTo Img
        GetClipboardText = DataObj.GetText
    On Error GoTo 0
Img:
    If Err = -2147221404 Then
        Err = 0
        Sheet1.Paste Destination:=Sheet1.Range("J55"), Link:=False
    Else
        'do nothing
    End If
    End Sub

这篇关于Excel-vba:仅从剪贴板粘贴图像+允许以后重复使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆