• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Win 10 VBA Code managing Imagen

AVG

New Member
I have stored some images in a worksheet (Lets say FOTOS1), the images are named Foto1, Foto2 etc. I want to place ONE imagen only in a Form Picture called PanelPrincipal.IMG_Description , I have succed to load the images in the PanelPrincipal.IMG_Description. If I do it directly from my hard disk, no problem, but when I tried from the FOTOS1 sheet I can't do it.

PanelPrincipal.IMG_Description.Picture = LoadPicture(C:\Somepicture.jpg). , so the panel ImageDescription works properly, then this code works perfect but the following code is not:

This is my current code that does not work the sentence img.SaveAsPicture tempFile does not save anything.

>>> use code - tags <<<
Code:
Sub LoadImage()
Dim rng As Range
Dim ws As Worksheet
Dim chrt As Object

    Windows(Archivo1a).Activate
    Sheets("FOTOS1").Select
    On Error Resume Next
    Set ws = ActiveWorkbook.Sheets("FOTOS1")
    On Error GoTo 0
    If ws Is Nothing Then
        MsgBox "Worksheet picture not found."
        Exit Sub
    End If
   
  Dim img As Picture
  On Error Resume Next
  Set img = ws.Pictures("Foto1")
'  Debug.Print img.picturedata   I have tried this but nothing was written in the inmediate panel
 
  If Not img Is Nothing Then
  img.SaveAs

        'Save image to temporary file
     Dim tempFile As String
    'tempFile = Environ$("temp") & "\temp.jpg"
        tempFile = "C:\temp.jpg"
'Another way to store the img      
'        Open tempFile For Binary Access Write As #1                        Note: I tried also to stored in binary format but Nothing was stored
'        Put #1, , img.PictureData
'        Close #1
'       Load image into form
        If Err.number <> 0 Then
            MsgBox "Error loading image: " & Err.description
        End If

        img.SaveAsPicture tempFile

        ' Load the image from the temporary file into the IMG_Description control
        PanelPrincipal.IMG_Description.Picture = LoadPicture(tempFile)
        If Err.number <> 0 Then
            MsgBox "Error loading image: " & Err.description
        End If
        'Clean up temporary file
        Kill tempFile
    Else
        MsgBox "Image does not exist in worksheet!"
    End If
End Sub


I have also tried the following code that does not work either

Code:
Sub LoadImageIntoForm()   
  Dim ws As Worksheet
   Dim img As Picture   

   Set ws = ThisWorkbook.Sheets("FOTOS1")       
   'Check if image exists   
   On Error Resume Next   
        Set img = ws.Pictures("Foto1")   
   On Error GoTo 0
       
   If Not img Is Nothing Then       
              'Load image into form       
              PanelPrincipal.IMG_Description.Picture = LoadPicture(img)    'But The LoadPicture expects a string pointing to a file not a picture   
   Else       
              MsgBox "Image does not exist in worksheet!"   
   End If

End Sub
Also y have tried the follow code
Code:
Sub LoadImage()
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = ThisWorkbook.Sheets("FOTOS1")
    On Error GoTo 0
    If ws Is Nothing Then
        MsgBox "Worksheet not found."
        Exit Sub
    End If
   
    Dim rng As Range
    Set rng = ws.Range("Foto1")
   
    Dim PanelP As Object
    Set PanelP = UserForms("PanelP") ' Replace "PanelP" with the name of your user form
   
    rng.CopyPicture ' Copy the image to the clipboard
    PanelP.IMG_Description.Picture = LoadPictureFromClipboard ' Paste the image into the image control
End Sub
'This is the LoadPictureFromClipboard
Function LoadPictureFromClipboard() As IPicture
    Dim objData As MSForms.DataObject
    Set objData = New MSForms.DataObject
    objData.GetFromClipboard
    Set LoadPictureFromClipboard = objData.GetFormat(2)
End Function
'On this last subroutine I had stop the code after store the picture or img in the clipboard but nothing or something different was in the clipboard.

So now I am desperate nothing seems to work as expected.
 
Last edited by a moderator:
The most expedient way to load the pictures from your worksheet into an image control on your userform is to first save the picture to an image file on your disk, then load it from there using the LoadPicture function. Example:
Code:
Private Sub CommandButton1_Click()
    Call LoadFromFile
End Sub

Private Sub CommandButton2_Click()
    Call LoadFromWorkSheet
End Sub

Private Sub LoadFromFile()
    Me.IMG_Description.Picture = LoadPicture("C:\Somepicture.jpg")  '<<< or whatever
End Sub

Private Sub LoadFromWorkSheet()
    Dim TmpPath As String
    Dim TmpChartObj As ChartObject
    Dim Pic As Shape

    With Worksheets("FOTOS1")
        For Each Pic In .Shapes
            If Pic.Name = "Picture 18" Then  '<<< or whatever
                TmpPath = VBA.Environ("Temp") & "\Tmp$$.jpg"
                Application.ScreenUpdating = False
                Pic.Copy
                Set TmpChartObj = .ChartObjects.Add(0, 0, Pic.Width, Pic.Height)
                TmpChartObj.Chart.Paste
                TmpChartObj.Chart.Export TmpPath
                TmpChartObj.Delete
                Application.ScreenUpdating = True
            End If
        Next Pic
    End With
    Me.IMG_Description.Picture = LoadPicture(TmpPath)
End Sub

The code above should be located in the code module for user form "PanelPrincipal"

84071

One last thing: please use code tags as I have done above. It makes your code easier to read.
 
Back
Top