View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Copy and Paste Charts as Picture in Different Workbook

Don't no if related, but:

I get an error: compile error: variable not defined

the debugger highlights this the I in: For I = 1 To
wsSource.ChartObjects.Count within the following modified code:


Afraid I have abosulutely no idea. The varaible wsSource is correctly
declared here -
Sub CopyChartsToPictures(wsSource As Worksheet, wsDest As Worksheet)

I can't think why -
wsSource.ChartObjects.Count
fails to compile, even if there are no chartobjects on the sheet. Should be
fine in all versions except conceivably in XL2007 which I don't have.

Last time I forgot to say to comment out these lines as the worksheet
references should have been set when calling this routine
' Set wsSource = Workbooks("Book2").Worksheets("Sheet1")
' Set wsDest = Workbooks("Book3").Worksheets("Sheet1")

Have you tested the original example in isolation.

Regards,
Peter T

"SteveC" wrote in message
...
I think I'm close.

Issue 1)
ThisWorkbook.Sheets(ShName) is the destination sheet where I want to paste
the charts (and everything else).

Another workbook / worksheet, Hot List.xls / Snapshot, is the source sheet
containing charts (and everything else).

I'll try to figure this out.

Issue 2)
Don't no if related, but:

I get an error: compile error: variable not defined

the debugger highlights this the I in: For I = 1 To
wsSource.ChartObjects.Count within the following modified code:



Sub CopyChartsToPictures(wsSource As Worksheet, wsDest As Worksheet)
Dim nPicCnt As Long
Dim chtObj As ChartObject
'Dim wsSource As Worksheet
'Dim wsDest As Worksheet
Dim pic As picture
'Dim I As Long

Set wsSource = Workbooks("Book2").Worksheets("Sheet1")
Set wsDest = Workbooks("Book3").Worksheets("Sheet1")

nPicCnt = wsDest.Pictures.Count

For I = 1 To wsSource.ChartObjects.Count
Set chtObj = wsSource.ChartObjects(I)

chtObj.CopyPicture Appearance:=xlScreen, Format:=xlPicture

wsDest.Paste

nPicCnt = nPicCnt + 1
With wsDest.Pictures(nPicCnt)
.Left = chtObj.Left
.Top = chtObj.Top
End With
Next

End Sub

"Peter T" wrote:

Hi Steve,

copying and pasting the charts as pictures would be embedded in a

larger
piece of code, which have the following dims already, which might

conflict
with your dims:


Although you could amend and embed my eample into your existing code

there's
anotrher way, referring to the example

Change -
Sub test()
to -
Sub CopyChartsToPictures(wsSource As Worksheet, wsDest As Worksheet)

Delete or comment the two Dim ref's to wsSource and wsDest

In your existing code add a line something like this -

CopyChartsToPictures ThisWorkbook.Sheets(ShName), ActiveSheet

If I follow, ThisWorkbook.Sheets(ShName) is the sheet containing charts

to
be copied as pictures and ActiveSheet is the sheet where they are to be
pasted. If not, amend the sheet references to suit.

For Each buttoms In ThisWorkbook.ActiveSheet.Shapes
buttoms.Delete
Next
this might be deleting the chart pictures as well? An MVP sent me a

link
to
a site showing how to delete buttons only, but haven't digged into to

it
yet.

Try simply -
ActiveSheet.Buttons.Delete

deletes all buttons inserted from the Forms menu (not ActiveX

Commandbuttons
from the Controls Toolbox menu)

Notice I didn't qualify ActiveSheet to ThisWorkbook (this file

containing
the code). The ActiveSheet can only be the the activesheet in the
activeworkbook, which may or may not be ThisWorkbook. Perhaps you need

to be
more explicit aboput which sheet you need to refer to.

Regards,
Peter T


"SteveC" wrote in message
...
Peter,

copying and pasting the charts as pictures would be embedded in a

larger
piece of code, which have the following dims already, which might

conflict
with your dims:

Dim I As Integer
Dim ShName As String
Dim Sht As Worksheet
Dim buttoms As Shape

since I"m a novice at vba, don't really know how to modify your code

without
pasting all my original code here (there's a lot).

here is a piece of code that might be simpler to embed in my existing

code:

Workbooks("Hot

List.xls").Sheets("Snapshot").Range("c48:i64").Cop yPicture _
Appearance:=xlScreen, Format:=xlPicture

But when I try to paste this into the new worksheet, it doesn't work:

With ThisWorkbook.Sheets(ShName).Range("c48").Select
ActiveSheet.PasteSpecial Format:="Picture (Enhanced Metafile)",
Link:=False _
, DisplayAsIcon:=False

Is there a quick fix for this, given the dim definitions above?

further on, there is this piece of code:

For Each buttoms In ThisWorkbook.ActiveSheet.Shapes
buttoms.Delete
Next

this might be deleting the chart pictures as well? An MVP sent me a

link
to
a site showing how to delete buttons only, but haven't digged into to

it
yet.

I can send you the entire code offline if you're interested...

thanks very much for your time and help.

Steve