What is the location of the picture loaded on userform

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

What is the location of the picture loaded on userform

Post by YasserKhalil »

Hello everyone
I have created a form with image control and used Picture property from the properties window to load a picture to save it on the form. I would like to know what is the exact location of the embedded picture? I changes the xlsm to zip and extract the files, expecting to find media folder but with no luck. I have found `drawings` folder but with no direct access to the picture

User avatar
HansV
Administrator
Posts: 78370
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: What is the location of the picture loaded on userform

Post by HansV »

The entire contents of the VBA project of the workbook, including the userform and its controls, is stored in a binary file named vbaProject.bin in the xl subfolder.

S2475.png

So you won't find the picture as a separate file.
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: What is the location of the picture loaded on userform

Post by YasserKhalil »

this code retrieve the picture from the userform

Code: Select all

SavePicture Image1.Picture, ThisWorkbook.Path & "\MySample.bmp"
How to change the embedded image by vba code so as to save the new one to the form?

User avatar
HansV
Administrator
Posts: 78370
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: What is the location of the picture loaded on userform

Post by HansV »

Sorry, I don't understand your question.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: What is the location of the picture loaded on userform

Post by YasserKhalil »

This code may clarify my target, this is a workaround

Code: Select all

Private Sub UserForm_Initialize()
    Dim ws As Worksheet, strFileName As String
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    If ws.Range("Z1").Value = Empty Then
        MsgBox "You Have To Select An Image First", vbInformation
        strFileName = Application.GetOpenFilename(FileFilter:="Tiff Files(.tif;.tiff),*.tif;*.tiff,JPEG Files (.jpg;.jpeg;*.jfif;*.jpe),*.jpg;*.jpeg;*.jfif;*.jpe,Bitmap Files(.bmp),.bmp", FilterIndex:=2, Title:="Select A File", MultiSelect:=False)
        If strFileName = "False" Then
            MsgBox "File Not Selected!"
        Else
            Me.Image1.Picture = LoadPicture(strFileName)
            Me.Repaint
            ws.Range("Z1").Value = strFileName
        End If
    Else
        Me.Image1.Picture = LoadPicture(ws.Range("Z1").Value)
        
    End If
End Sub

User avatar
HansV
Administrator
Posts: 78370
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: What is the location of the picture loaded on userform

Post by HansV »

I don't understand what the problem is.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: What is the location of the picture loaded on userform

Post by YasserKhalil »

What I am trying to do is the same exact manual steps to embed image into the userform, but instead of doing that manually, I need a code that loads the picture and after executing the code and closing the userform, I need to find the image that I loaded in the design view. Hope it is clear now.

User avatar
HansV
Administrator
Posts: 78370
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: What is the location of the picture loaded on userform

Post by HansV »

Thanks!

Code: Select all

Sub Test()
    Dim ws As Worksheet
    Dim strFileName As String
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    strFileName = ws.Range("Z1").Value
    If strFileName = "" Then
        MsgBox "You Have To Select An Image First", vbInformation
        strFileName = Application.GetOpenFilename _
            (FileFilter:="Tiff Files(.tif;.tiff),*.tif;*.tiff," & _
            "JPEG Files (.jpg;.jpeg;*.jfif;*.jpe),*.jpg;*.jpeg;*.jfif;*.jpe," & _
            "Bitmap Files(.bmp),.bmp", FilterIndex:=2, Title:="Select A File")
        If strFileName = "False" Then
            MsgBox "File Not Selected!", vbExclamation
            Exit Sub
        Else
            ws.Range("Z1").Value = strFileName
        End If
    End If
    ThisWorkbook.VBProject.VBComponents("Userform1").Designer _
        .Controls("Image1").Picture = LoadPicture(strFileName)
End Sub
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78370
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: What is the location of the picture loaded on userform

Post by HansV »

Don't forget to save the workbook after running this macro.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: What is the location of the picture loaded on userform

Post by YasserKhalil »

Great my tutor.
I tried to use the code on the userform through command button like that

Code: Select all

Private Sub CommandButton1_Click()
    Dim strFileName As String
    strFileName = Application.GetOpenFilename _
        (FileFilter:="Tiff Files(.tif;.tiff),*.tif;*.tiff," & _
        "JPEG Files (.jpg;.jpeg;*.jfif;*.jpe),*.jpg;*.jpeg;*.jfif;*.jpe," & _
        "Bitmap Files(.bmp),.bmp", FilterIndex:=2, Title:="Select A File")
    If strFileName = "False" Then
        MsgBox "File Not Selected!", vbExclamation
        Exit Sub
    Else
        ThisWorkbook.VBProject.VBComponents("UserForm1").Designer.Controls("Image1").Picture = LoadPicture(strFileName)
    End If
End Sub

But I got an error which is [Object variable or With block variable not set]

User avatar
HansV
Administrator
Posts: 78370
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: What is the location of the picture loaded on userform

Post by HansV »

You want to change the design of the userform. You cannot do this while the userform is running. You have to run the macro while the userform is not open.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: What is the location of the picture loaded on userform

Post by YasserKhalil »

Thanks a lot for the information.
Best Regards