Macro Excel - Save graphic image export to directory and import to form

Asked

Viewed 573 times

1

I updated my Office version for 2016 and some macros stopped working properly. The routine when executed in isolation works properly, but when it enters a process it stops working. The macro in question makes the following statements. Copies a Range:

Set rng = .Range("S1:W4")
rng.CopyPicture xlScreen, xlBitmap 

Creates a chart:

Set cht = .ChartObjects.Add(0, 0, rng.Width, rng.Height)

Pastes the image on the chart Export the image in JPGEG format to a folder.

As informed, the routine works if used separately. But when I run the form, the routine stops working. The chart is exported but no image. The file size is also reduced.

  • All indications are that the problem is not a version update necessarily, but adverse conditions execution. Your call .Range("S1:W4"), for example, implicitly executes the function Range to the active spreadsheet tab (ActiveWorksheet). Maybe the moment you execute this call "within the form" or "within a process" (whatever that means, since you didn’t contextualize it properly), there is no active tab or it is not the expected one. I suggest changing the code to invoke the function in the correct tab.

1 answer

2

I was able to identify the problem: In the previous version I copied the interval image, created a graph with the dimensions of the interval and pasted the copied image directly on the graph.

the problem solved by including the command cht.Activate before the procedure cht.Chart.Paste.

  • Not at all. I’m glad you were able to solve it. But I edited your answer to take the "thank you" and "cordially". It’s just that this site is not a forum. If you haven’t done it yet, do the [tour], okay? And, welcome to SOPT! :)

  • P.S.: As it was yourself who answered, you still can not accept the answer (the system prevents this for a couple of days, I think, to motivate other people to answer too). But be sure to come back here and accept the answer after that period, ok? It is important to actually mark your question as completed and help other people with similar problems.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.