开发者

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

开发者 https://www.devze.com 2023-01-08 07:49 出处:网络
In Excel 2003, I need to copy a Graphics object (sheet.PageSetup.LeftFooterPicture开发者_StackOverflow中文版) to the Clipboard.

In Excel 2003, I need to copy a Graphics object (sheet.PageSetup.LeftFooterPicture开发者_StackOverflow中文版) to the Clipboard.

How can I do that?


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


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.


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)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消