Forum Discussion
Oliversanabia
May 11, 2022Copper Contributor
Vba Incert a image on a image (activex control)
Hello,
I have being trying to insert a image in a image (activex control). I managed to find a code that lets me press a button that opens the file explorer so that I can select the image in question and subsequently place it in the image (activex control) and that’s fine. The thing is that I need this button to open a specific folder where all the images are placed so I can select the image in question. Also, this code that I’ll present below doesn’t let me select images that are in .png format, only .jpg.
This is the code I found:
Private Sub CommandButton1_Click()
Dim x As Integer
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
x = Application.FileDialog(msoFileDialogOpen).Show
If x <> 0 Then
fpath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
Image1.Picture = LoadPicture(fpath)
Image1.PictureSizeMode = fmPictureSizeModeStretch
End If
End Sub
Like this:
Private Sub CommandButton1_Click() With Application.FileDialog(msoFileDialogOpen) .Filters.Clear .Filters.Add "Picture files (*.jpg, *.png, *.gif)", "*.jpg,*.png,*.gif" .InitialFileName = "C:\yourfolder\*.jpg" If .Show Then Image1.Picture = LoadPicture(.SelectedItems(1)) End If End With End Sub