blank image control

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
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!
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,292
Members
448,885
Latest member
LokiSonic

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