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 <<<
I have also tried the following code that does not work either
Also y have tried the follow code
'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.
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
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
So now I am desperate nothing seems to work as expected.
Last edited by a moderator: