Run time error 481 - Invalid Picture

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
217
Office Version
  1. 2016
Platform
  1. Windows
Hello experts,

I am using a user form to upload two charts, having a option button to switch between them, however I am not able to upload the second chart on the user form, keep getting the - Run time error 481 - Invalid Picture

The code works fine for the first pic, saving it in the correct directory (below), but the second chart the file is saved but with 0 bytes.

I have enough space in my temp folder so space is not an issue.

VBA Code:
Sub LoadForm()
    FormCharts.Show
End Sub

Sub ChangeChart (ChartName As String)
    Dim CurrentChart As Chart
    Dim FName As String
  
    FName = ThisWorkbook.Path & "\temp.gif"  
    Set CurrentChart = ThisWorkbook.Sheets("Charts").ChartObjects(ChartName).Chart  
    CurrentChart.Export Filename:=FName, filtername:="GIF" 
    FormCharts.ImgChart.Picture = LoadPicture(FName)
End Sub



Form code:
VBA Code:
Private Sub Close_Click()

Unload Me

End Sub


Private Sub Opt2WksCov_Click()

Call ChangeChart("Chart2wks")

End Sub


Private Sub Opt3WksCov_Click()

Call ChangeChart("Chart3wks")

End Sub


Private Sub UserForm_Initialize()

    Opt3WksCov.Value = True

End Sub
 

Attachments

  • UserForm.PNG
    UserForm.PNG
    123.1 KB · Views: 22
Last edited by a moderator:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Maybe because the file names are the same? Try adding a variable like a timestamp to the export and see if that works?
 
Upvote 0
Hi @ismii,

Thanks for the suggestion, I have added a timestamp variable but the issue is still happening.

Sub ChangeChart(ChartName As String)

Dim CurrentChart As Chart
Dim FName As String
Dim dt As String

dt = Format(CStr(Now), "yyy_mm_dd_hh_mm")
FName = ThisWorkbook.Path & "\" & dt & "temp.gif"

'On Error Resume Next
'On Error GoTo 0

Set CurrentChart = ThisWorkbook.Sheets("Charts").ChartObjects(ChartName).Chart

CurrentChart.Export Filename:=FName, filtername:="GIF"

FormCharts.ImgChart.Picture = LoadPicture(FName)


End Sub
 
Upvote 0
@ismii,

I am activating the chart before exporting and it is working ok now.

Sub ChangeChart(ChartName As String)

Dim CurrentChart As Chart
Dim FName As String
Dim dt As String

dt = Format(CStr(Now), "yyy_mm_dd_hh_mm")
FName = ThisWorkbook.Path & "\" & dt & "_temp.gif"

'On Error Resume Next
'On Error GoTo 0

Set CurrentChart = ThisWorkbook.Sheets("Charts").ChartObjects(ChartName).Chart

ThisWorkbook.Sheets("Charts").ChartObjects(ChartName).Activate

CurrentChart.Export Filename:=FName, filtername:="GIF"

FormCharts.ImgChart.Picture = LoadPicture(FName)
 
Upvote 0
Glad to hear!

Unknown on my end why that would work, but I've seen some funky stuff with charts, ribbon, and other 'things' that don't deal strictly with the sheet.
 
Upvote 0
Thanks and saved me lot of time
@ismii,

I am activating the chart before exporting and it is working ok now.

Sub ChangeChart(ChartName As String)

Dim CurrentChart As Chart
Dim FName As String
Dim dt As String

dt = Format(CStr(Now), "yyy_mm_dd_hh_mm")
FName = ThisWorkbook.Path & "\" & dt & "_temp.gif"

'On Error Resume Next
'On Error GoTo 0

Set CurrentChart = ThisWorkbook.Sheets("Charts").ChartObjects(ChartName).Chart

ThisWorkbook.Sheets("Charts").ChartObjects(ChartName).Activate

CurrentChart.Export Filename:=FName, filtername:="GIF"

FormCharts.ImgChart.Picture = LoadPicture(FName)
s
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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
Back
Top