PDA

View Full Version : Excel 2010 chart's error



Benzadeus
08-22-2011, 04:55 AM
Hello all,

I use a code to export a chart object to a file:
Sub CarregarFigura(img, sGráfico As String)
Dim sArquivo As String

sArquivo = ThisWorkbook.Path & "\temp.bmp"
Sheets("Gráficos").ChartObjects(sGráfico).Chart.Export Filename:=sArquivo, FilterName:="BMP"

With img
.Picture = LoadPicture(sArquivo)
End With

Kill sArquivo
End Sub

I get an error 481: Invalid picture (translated from portuguese) at line:
.Picture = LoadPicture(sArquivo)


Well, when I go to the sheet that has the charts and CLICK them manually, one by one, the same code works thereafter. Anyone had this issue ever?

mancubus
08-22-2011, 06:09 AM
hi.

does this help:
http://www.vbaexpress.com/kb/getarticle.php?kb_id=449

Aflatoon
08-22-2011, 07:01 AM
I have not had that issue, though I think I have seen a previous post somewhere that was reminiscent of your issue. I will see if I can locate it in case there were a resolution.
For the moment however, have you tried exporting as a different format, or specifically setting ScreenUpdating to true before you run this code. Additionally, do you have SP1 installed?

Kenneth Hobs
08-22-2011, 08:27 AM
You set img as a Variant. What type of object is it? Where is it located, ActiveSheet, Userform, etc.?

Benzadeus
08-22-2011, 02:17 PM
Thank you for the aswers. I attached an example, I'd like you to test this Workbook.
I'd like to add that this error occured at 3 different PCs: 2 with Excel 2010-64bit and 1 with Excel 2010-32bit.

@mancubus
I already use this link's code (adapted) on my procedure.

@Aflatoon
I tried others formats too, having the same issue. My Office is not SP1. I already run this code with ScreenUpdate=False. The chart object is at a hidden worksheet.

@Kenneth Hobs
The img is a Image control, at a UserForm.
I set that as Variant because I use the same code to put the image on a UserForm, but that's another history.

Paul_Hossler
08-22-2011, 06:48 PM
Your attachment works fine for me (Vista, Excel 2010, 32 bit)

Paul

Aflatoon
08-23-2011, 01:16 AM
And also for me (Windows XP, Office 2010 32bit with SP1) - I tested all the names in the dropdown just to be sure.

Have you tested the exported images to see if you can open them in a typical image viewer without error?

Bob Phillips
08-23-2011, 01:29 AM
And here, Windows 7 Ultimate, Excel 2010 SP1.

Aflatoon
08-23-2011, 03:06 AM
This (http://social.msdn.microsoft.com/Forums/pl-PL/isvvba/thread/736eeb5f-7f21-4006-87ee-7748f3c83175) is the post I was thinking of, which I mentioned earlier.

Kenneth Hobs
08-23-2011, 05:44 AM
Based on the link that Aflatoon posted, set your code to Select the chart object first.

Benzadeus
08-23-2011, 06:52 AM
Thank for your time testing it.
I attached another Workbook, this time I bet you'll get some trouble trying tot load it :)

@Aflatoon
That topic is exactly what I needed. I'll spend some time studying it.

Aflatoon
08-23-2011, 07:12 AM
Does this version work for you?

Benzadeus
08-23-2011, 07:15 AM
I created a workaround... with this code, I got it working (put on Workbook Open event):

Private Sub Workbook_Open()
Travar
Application.WindowState = xlMaximized
LoopGráficos
End Sub

Sub LoopGráficos()
Dim ws As Worksheet
Dim co As ChartObject

Set ws = Sheets("Gráficos")

With ws
.Visible = xlSheetVisible
.Activate
For Each co In ws.ChartObjects
co.Select
Next co
.Range("A1").Select
End With

Sheets("Início").Activate
End Sub

As you can see, I selected all charts when the workbook was opened. Worked for me.

Benzadeus
08-23-2011, 07:17 AM
@Aflatoon
Your version did not work. I had to scroll all graphs object in order to make it work.

Benzadeus
08-23-2011, 08:41 AM
This is the final version of my workaround:

Private Sub Workbook_Open()
If CLng(Val(Application.Version)) = 14 Then
LoopGráficos Sheets("Gráficos")
'LoopGráficos Sheets("Gráficos2")
'LoopGráficos Sheets("Gráficos3")
'...
'Loop in every sheet that has a chart object
'that'll be used on a UserForm
End If
End Sub

Sub LoopGráficos(ws As Worksheet)

Dim co As ChartObject
Dim blScreenUpdating As Boolean
Dim sv As XlSheetVisibility

blScreenUpdating = Application.ScreenUpdating
Application.ScreenUpdating = True

With ws
sv = .Visible
.Visible = xlSheetVisible
.Activate
For Each co In .ChartObjects
co.Select
Next co
.Visible = sv
End With

Application.ScreenUpdating = blScreenUpdating

End Sub

I tought about scrolling the window to "view" each of the chart objects, but I think that'll not be necessary.

Also, notice that the code runs only for Excel 2010 versions (32 and 64 bit).