vba works if stepping but not playing - at Chart.Paste

mtharnden

Board Regular
Joined
Aug 17, 2011
Messages
114
Hello,

I have a code that works perfectly every time when I step through it, however when I play it normally it does not. It will save a white image rather than the range.
This code saves a range as a .PNG. It took me forever to find one that worked the way I want it to. What I like about this method is it works zooming in and saving the file as a large image as I am displaying them on large TV displays.

I think it is the chart.paste piece that messes up. If a play up to that then step it works, if I play just past it then step it does not work.

I have tried different methods of this that have not worked in the way I like.

Code:
Sub test2()


    Set Sheet = Worksheets("test_sheet")
    Sheets("test_sheet").Select
    output = "C:\Users\matthew.harnden\Documents\test_image.png"
    zoom_coef = 100 / Sheet.Parent.Windows(1).Zoom * 4
    Set area = Worksheets("test_sheet").Range("B7:O30")
    area.CopyPicture xlBitmap
    Set chartobj = Sheet.ChartObjects.Add(0, 0, area.Width * zoom_coef, area.Height * zoom_coef)
    chartobj.Chart.Paste
    chartobj.Chart.Export output, "png"
    chartobj.Delete


End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,520
After you've created your chartobject and assigned it to chartobj, try activating the chartobject before exporting it...

Code:
chartobj.Activate
 

mtharnden

Board Regular
Joined
Aug 17, 2011
Messages
114
After you've created your chartobject and assigned it to chartobj, try activating the chartobject before exporting it...

Code:
chartobj.Activate


sir, you are amazing! i placed that before the paste and it worked. thank you very much!
 

Watch MrExcel Video

Forum statistics

Threads
1,130,170
Messages
5,640,568
Members
417,151
Latest member
ChickenTenderer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top