如何在 VBA 中将图片(图形对象)复制到剪贴板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3317437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:55:21  来源:igfitidea点击:

How to copy a picture (graphic object) to the Clipboard in VBA?

excelvbams-office

提问by Arseny

In Excel 2003, I need to copy a Graphics object (sheet.PageSetup.LeftFooterPicture) to the Clipboard.

在 Excel 2003 中,我需要将 Graphics 对象 ( sheet.PageSetup.LeftFooterPicture)复制到剪贴板。

How can I do that?

我怎样才能做到这一点?

回答by barrowc

In Excel versions prior to 2007, you cannot extract the graphic from the footer. You would need to have the original image file.

在 2007 之前的 Excel 版本中,您无法从页脚中提取图形。您需要拥有原始图像文件。

See this previous questionfor more details

有关更多详细信息,请参阅上一个问题

回答by Arseny

as it mentioned before the problem is that I cannot extract picture from graphic object(LeftFooterPicture) Looking on the answers I did muddle through this issue.

正如它之前提到的问题是我无法从图形对象(LeftFooterPicture)中提取图片查看答案我确实对这个问题感到困惑。

The Filename should contain the whole path to the file like 'C:\myimage.jpg' but once the worksheet is saved it changed the filename to 'myiamge' without the path and the extension.

文件名应包含文件的完整路径,如“C:\myimage.jpg”,但一旦保存工作表,它将文件名更改为“myiamge”,而没有路径和扩展名。

so here is my workaround:

所以这是我的解决方法:

  1. Before 'Save' event I scan all worksheets and get their Graphic to that time "Filename" has right content (absolute path like C:\mypic.jpg)
  2. I create a hiden worksheet and add all pictures as Shape objects (Shapes.AddPicture with picture's path)

  3. I bind a current workshhet name and picture position with shape name

  4. By the time I need to copy a picture to cliapboard I look up the picture in the hiden page (shape.CopyPicture xlScreen, xlPicture)

  1. 在“保存”事件之前,我扫描所有工作表并将它们的图形设置为“文件名”具有正确的内容(绝对路径如 C:\mypic.jpg)
  2. 我创建了一个隐藏工作表并将所有图片添加为 Shape 对象(Shapes.AddPicture 与图片的路径)

  3. 我将当前的工作名称和图片位置与形状名称绑定

  4. 当我需要将图片复制到剪贴板时,我会在隐藏页面中查找图片(shape.CopyPicture xlScreen,xlPicture)

回答by marg

According to MSDNa Graphicloads the Image through a file (filename). The Filenameshould contain the whole path to the file like 'C:\myimage.jpg' but once the worksheet is saved it changed the filename to 'myiamge' without the path and the extension. I wasn't able to find any other Reference to the file within Excel.

根据MSDN,aGraphic通过文件(文件名)加载图像。本Filename应包含完整路径,如文件“C:\ myimage.jpg”但一旦表被保存它改变了文件名以“myiamge”不带路径和扩展。我无法在 Excel 中找到对该文件的任何其他引用。

The following code might help you.

以下代码可能对您有所帮助。

Sub yourMethod()
    copyGraphic Me.PageSetup.LeftFooterPicture
End Sub

Sub copyGraphic(srcGraphic As Graphic)
    Dim imagefolder As String
    Dim imageExtension As String
    Dim imagePath As String

    imagefolder = "D:\" '"
    imageExtension = ".gif"

    If InStr(1, srcGraphic.filename, ".") Then
        imagePath = srcGraphic.filename
    Else
        imagePath = imagefolder & srcGraphic.filename & imageExtension
    End If

    Me.Shapes.AddPicture imagePath, False, True, 10, 10, Round(srcGraphic.Width, 0), Round(srcGraphic.Height, 0)
End Sub

You might want to change Me.to the Name of your Sheet and the Destination Sheet.

您可能想要更改Me.为您的工作表的名称和目标工作表。

回答by oopbase

Private Sub Command1_Click()
Clipboard.Clear
Clipboard.SetData Picture1.Picture, vbCFBitmap
Command1.Enabled = False
End Sub

Like this you can copy pictures to the clipboard.

像这样您可以将图片复制到剪贴板。

回答by Debbie

When I try to copy something that doesn't seem to want to copy, I do a Print Screen from my keyboard and then paste it to the PAINT accessory. From there, you can crop out anything you don't want and then cut and paste the new image into a new PAINT file so it is clean. You can save it in a .jpg for easier use.

当我尝试复制似乎不想复制的内容时,我会从键盘执行 Print Screen,然后将其粘贴到 PAINT 附件。从那里,您可以裁剪掉您不想要的任何内容,然后将新图像剪切并粘贴到新的 PAINT 文件中,使其保持干净。您可以将其保存为 .jpg 以便于使用。

Hope that helps.

希望有帮助。