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

发布于 2024-09-11 19:07:38 字数 115 浏览 3 评论 0原文

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

我怎样才能做到这一点?

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

How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青春有你 2024-09-18 19:07:45
Private Sub Command1_Click()
Clipboard.Clear
Clipboard.SetData Picture1.Picture, vbCFBitmap
Command1.Enabled = False
End Sub

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

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.

神仙妹妹 2024-09-18 19:07:44

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

希望有帮助。

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.

Hope that helps.

遗忘曾经 2024-09-18 19:07:43

正如之前提到的,问题是我无法从图形对象中提取图片(LeftFooterPicture)
看了答案,我确实解决了这个问题。

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

所以这是我的解决方法:

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

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

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

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.

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.

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)

荒岛晴空 2024-09-18 19:07:42

根据 MSDN 一个 Graphic 通过文件(文件名)加载图像。 文件名 应包含文件的完整路径,如“C:\myimage.jpg”,但保存工作表后,它将文件名更改为“myiamge”,不包含路径和扩展名。我无法在 Excel 中找到对该文件的任何其他引用。

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

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

您可能希望将 Me. 更改为您的工作表名称和目标工作表。

According to MSDN a Graphic loads the Image through a file (filename). 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. I wasn't able to find any other Reference to the file within 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.

朮生 2024-09-18 19:07:41

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

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

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

See this previous question for more details

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文