VBA Exporting sheet range as image sometimes gives blank image

Jack113256

New Member
Joined
Dec 9, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi, im trying to export the sheet as an image but theres a small chance that the output jpg file will be blank.
Is it a timing issue or something? Ive tried putting delays after exporting the image but the error still occurs sometimes.

For your info, The sheet that contains this range is not empty and contains a chart,

VBA Code:
    ws.Activate

    Dim rng As Range
    Set rng = Range("A1:Q25")
    rng.CopyPicture xlScreen, xlPicture

    Set oCht = ActiveSheet.ChartObjects.Add(Left:=0, Top:=0, Width:=rng.Width, Height:=rng.Height)
    oCht.Activate
    With oCht.Chart
        .Paste
        .Export fileName:=picPathJpg, Filtername:="JPG"
    End With
  
    wb.Close SaveChanges:=False
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi Jack and Welcome to the Board! I'm not that sure that this code is much different but U can try it out and see if it helps. Dave
Code:
Application.ScreenUpdating = False
ws.Activate
Dim rng As Range
Set rng = ws.Range("A1:Q25")
rng.CopyPicture ' xlScreen, xlPicture
With ActiveSheet.ChartObjects.Add(Left:=0, Top:=0, Width:=rng.Width, Height:=rng.Height)
.Activate
.Chart.Paste
.Chart.Export Filename:=picPathJpg, Filtername:="JPG"
End With
Application.ScreenUpdating = True
wb.Close SaveChanges:=False
 
Upvote 0
Hi Jack and Welcome to the Board! I'm not that sure that this code is much different but U can try it out and see if it helps. Dave
Code:
Application.ScreenUpdating = False
ws.Activate
Dim rng As Range
Set rng = ws.Range("A1:Q25")
rng.CopyPicture ' xlScreen, xlPicture
With ActiveSheet.ChartObjects.Add(Left:=0, Top:=0, Width:=rng.Width, Height:=rng.Height)
.Activate
.Chart.Paste
.Chart.Export Filename:=picPathJpg, Filtername:="JPG"
End With
Application.ScreenUpdating = True
wb.Close SaveChanges:=False
Ok i'll try, thanks.
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,107
Members
452,302
Latest member
TaMere

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