views:

219

answers:

5

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

How can I do that?

A: 
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.

Forlan07
"Picture1.Picture" what's that? how can I pass a picture from LeftFooterPicture property to that object?
Arseny
Picture1 is an image. I didn't use LeftFooterPicture before, but maybe"Clipboard.SetData sheet.PageSetup.LeftFooterPicture.Picture, vbCFBitmap" works?
Forlan07
@Forlan07 no It dosen't work. LeftFooterPicture is Graphic typed object and I cannot see any property there to extract an image from it.
Arseny
@Arseny: Ok, I just googled a little, maybe this will work:"Clipboard.SetDataObject(LeftFooterPicture)"?I got this from a german vba page, and it says the function can copy any object to the clipboard.Greetings
Forlan07
+1  A: 

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.

marg
thanks the replay but still dosen't work. AddPicture raises exception.BTW imagePath has to point to the existed file on a disk. when I call AddPicture there is no shuch file on d:\
Arseny
As I said in the answer. When I saved the Excel sheet he changed the path that was stored in .filename to the filename without the fileextension. For example D:\myimage.gif became myimage. Since I can not track back where the Image was from I took the detour of seting the folder and extension by hand (variables: imagefolder and imageextension). As for the error: try to debug.print imagePath and look if the File exists otherwise The Macro can't add it to your Sheet. The solution is not really good but it might help ^^
marg
@marg the problem is that i have only excel file on my PC and all pictures of Pagesetup are embedded into this file.
Arseny
+2  A: 

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

barrowc
+1  A: 

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)

Arseny
A: 

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.

Debbie
Thanks Debbie. Got the idea but I need to do it automatically using VBA
Arseny