blank image control

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
969
I have this code that takes a PNG picture on a sheet and saves it as a GIF image and then uploads to a userform image. Sometimes it works, but most of the time, I get this image? Can someone tell me why this image appears?

1603302497222.png


VBA Code:
Private Sub tgImage_Click()
Dim MyChart As String, MyPicture As String, FName As String
Dim PicWidth As Long, PicHeight As Long, Pict As Object
Dim oChartObj As ChartObject


If tgImage = False Then
    Image2.Picture = LoadPicture("")
    Image2.Visible = False
Else
    Application.ScreenUpdating = False
    Set Pict = ActiveSheet.Pictures.Insert(ActiveCell.Comment.Text)
    
    With Pict
        .ShapeRange.LockAspectRatio = msoTrue
        .ShapeRange.Width = 650
        .ShapeRange.Height = 650
    End With
    
    Pict.Name = ActiveCell.Value
    MyPicture = ActiveCell.Value
    
    Set oChartObj = ActiveSheet.ChartObjects.Add(Top:=0, Left:=0, Width:=600, Height:=300)
    
    With ActiveSheet
        .Shapes(MyPicture).Copy
    
        With oChartObj.chart
              .ChartArea.Select
              .Paste
        End With
        
        FName = VBA.Environ("TEMP") & Application.PathSeparator & "MyPic.Gif"
        .ChartObjects(1).chart.Export FName
    
        Image2.Visible = True
        Image2.Picture = LoadPicture(FName)
        
        oChartObj.Delete
        .Shapes(MyPicture).Delete
        Kill FName
    End With
End If
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,495
Try activating the chartobject...

VBA Code:
        With oChartObj
            .Activate
            With .Chart
                .ChartArea.Select
                .Paste
            End With
        End With

Actually, the last part of your code can be re-written as follows...

Code:
    FName = VBA.Environ("TEMP") & Application.PathSeparator & "MyPic.Gif"
        
    ActiveSheet.Shapes(MyPicture).Copy
    
    Set oChartObj = ActiveSheet.ChartObjects.Add(Top:=0, Left:=0, Width:=600, Height:=300)
    
    With oChartObj
          .Activate
          With .Chart
            .Paste
            .Export FName, "GIG"
          End With
          .Delete
    End With

    Image2.Visible = True
    Image2.Picture = LoadPicture(FName)
    
    ActiveSheet.Shapes(MyPicture).Delete
    
    Kill FName

Hope this helps!
 

Watch MrExcel Video

Forum statistics

Threads
1,129,479
Messages
5,636,574
Members
416,925
Latest member
malamutus

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