Run time error 481 - Invalid Picture

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
99
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: 11
Last edited by a moderator:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

ismii

Board Regular
Joined
Feb 5, 2015
Messages
150
Maybe because the file names are the same? Try adding a variable like a timestamp to the export and see if that works?
 

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
99
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
 

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
99
@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)
 

ismii

Board Regular
Joined
Feb 5, 2015
Messages
150
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.
 

sureshtrb

Board Regular
Joined
Mar 24, 2013
Messages
106
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
 

Watch MrExcel Video

Forum statistics

Threads
1,129,490
Messages
5,636,628
Members
416,932
Latest member
mm07

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