Import sheet charts to userform (excel vba)

Asked

Viewed 1,102 times

0

I have to import 3 charts for a userform. I had even managed, but suddenly started to give errors, then I put the code "On Error Resume Next" (but this is not solution, just does not lock my form anymore, but does not do what should do). At the moment I have the code below:

Sheets("Plan3").Select

On Error Resume Next

Set Grafico = Sheets("Plan3").ChartObjects("Graf").Chart

nome = "C:\Windows\Temp" & Application.PathSeparator & "temp.gif"
Grafico.Export Filename:=nome, Filtername:="GIF"
Imagem1.Picture = LoadPicture(nome)

Sheets("Historico").Select

On Error Resume Next

Set Grafico = Sheets("Historico").ChartObjects("Grafi").Chart

nome = "C:\Windows\Temp" & Application.PathSeparator & "temp.gif"
Grafico.Export Filename:=nome, Filtername:="GIF"
Imagem2.Picture = LoadPicture(nome)

Sheets("KalosG1").Select

On Error Resume Next

Set Grafico = Sheets("KalosG1").ChartObjects("Grafic").Chart

nome = "C:\Windows\Temp" & Application.PathSeparator & "temp.gif"
Grafico.Export Filename:=nome, Filtername:="GIF"
Imagem3.Picture = LoadPicture(nome)

Note: I put "Sheets("...."). Select" before each "On Error Resume Next" to try to solve, but it did not work. Ps: there are hours that work have hours that do not. Sometimes only one of the graphics fails... I have no idea what is wrong.

  • Instead of ignoring the error (which is what the command On Error Resume Next) makes, fix the error! If the error is occurring, it is because some problem exists. Without you post what is this error, it is impossible for anyone to help you.

  • Put the code snippet in a Try / catch block, take the Exception message and click here.

  • Good morning Luiz Vieira. I took the "On error Resume Next" and the error that appeared was: Error at runtime '481': Invalid figure

  • Good morning Marcell Alves. Boy, I’m sorry for the ignorance, but I know if I know what this block would be Try / catch. However the error that appeared was: Runtime error '481': Invalid figure

  • Just to be sure, the "c: windows temp" directory exists, right? Also, the charts and spreadsheets are with the correct names, right? I ask because this error indicates that the LoadPicture is failing, which could mean that the previous code failed to save the figure with the chart.

  • Good afternoon Luiz Vieira. Yes. At first it’s all right (so much so that the error is not continuous... There are hours that occurs and hours that do not)... Ps: a guy said that this is a kind of "bug" (he didn’t say exactly that) from the office itself. Dai he suggested by: "Grafico.Parent.Select " below the part: name = "C: Windows Temp" & Application.Pathseparator & "temp.gif" ... Well, it seems to have worked, since yesterday that no more failure.

Show 1 more comment
No answers

Browser other questions tagged

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