Error: Invalid Picture in Userform

AlwaysNeedExcelHelp

New Member
Joined
Sep 24, 2019
Messages
6
Hello all-

I have a userform which is taking pictures of graphs and displaying it in excel with the below code. Occasionally I get a Run Time Error 481 Invalid Picture on the line below. What's odd is when I go in and change the name of the charts in both the excel and the vba code, everything works smooth but every new excel instance causes this, any idea?





Sub Changechart(chartname As String)

Dim CurrentChart As Chart
Dim FName As String

FName = ThisWorkbook.Path & "\temp.bmp"

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

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

frmCharts.imgChart.Picture = LoadPicture(FName)


End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try activating the chart before exporting it. For example, add the following line prior to exporting the image..

Code:
CurrentChart.Parent.activate

Does this help?
 
Upvote 0
Hmmmm, you shouldn't have to activate the chart before exporting it. But sometimes the export takes longer for Windows to process, and the VBA gets ahead of itself.

Rather than activating charts, which leads to flashing of items on screen while the code runs, I often insert a "DoEvents" in between time-consuming steps:

Code:
CurrentChart.Export Filename:=FName, filtername:="BMP"

DoEvents

frmCharts.imgChart.Picture = LoadPicture(FName)

When I'm feeling very ambitious, I will wrap the LoadPicture command inside a loop, after an OnError Resume Next, If there's an error, I go back and try the LoadPcture again, otherwise it succeeded, and I exit the loop.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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