Option Explicit
Private Sub cboSKU_Change()
Dim pic As Picture
Dim imageFileName As String
Dim saveAsFileName As String
Dim errMsg As String
If Me.cboSKU.ListIndex = -1 Then
Me.ImageSKU.Picture = LoadPicture("")
Exit Sub
End If
imageFileName = Me.cboSKU.Value
On Error Resume Next
Set pic = ThisWorkbook.Worksheets("Parts").Pictures(imageFileName) 'change the sheet name accordingly
If pic Is Nothing Then
MsgBox "'" & imageFileName & "' does not exist!", vbExclamation
Exit Sub
End If
On Error GoTo 0
saveAsFileName = Environ("temp") & "\temp.jpg"
If Not ExportShapeToImage(pic, saveAsFileName, errMsg) Then
MsgBox errMsg, vbCritical, "Error"
Exit Sub
End If
Me.ImageSKU.Picture = LoadPicture(saveAsFileName)
Kill saveAsFileName
End Sub
Function ExportShapeToImage(ByVal shapeToExport As Object, ByVal saveAsFileName As String, ByRef errorMessage As String) As Boolean
On Error GoTo errorHandler
shapeToExport.CopyPicture appearance:=xlScreen, Format:=xlBitmap
Dim tempWorksheet As Worksheet
Set tempWorksheet = ThisWorkbook.Worksheets.Add
With tempWorksheet.ChartObjects.Add(Left:=0, Top:=0, Width:=shapeToExport.Width, Height:=shapeToExport.Height)
.Activate
With .Chart
.ChartArea.Format.Line.Visible = msoFalse
.Paste
.Export fileName:=saveAsFileName
End With
'.Delete
End With
Application.DisplayAlerts = False
tempWorksheet.Delete
Application.DisplayAlerts = True
ExportShapeToImage = True
Exit Function
errorHandler:
errorMessage = "Error " & Err.Number & ":" & vbCrLf & vbCrLf & Err.Description
ExportShapeToImage = False
End Function