[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 128
  • Last Modified:

Image place on userform by VBA code is lost when file is saved

Hi all, I have a spreadsheet with operations perfomed by VBA code on several userforms. I have code that allows a user to select a picture and place it on a user form as image1. The code works properly and users can navigate to a graphic file, select the file and the image is placed on the userfom. However when the file is saved and reopened the image is not on the form.
Can anyone tell me how to alter my code of add code so the image placed on the userformis not lost when the file is saved.

My routine is:

Private Sub CommandButton2_Click()

Dim openDialog As Office.FileDialog
Dim FName As String

Set openDialog = Application.FileDialog(msoFileDialogFilePicker)
openDialog.Filters.Add "JPEG Files", "*.jpg"

     With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .ButtonName = "Submit"
        .Title = "Select an image file"
        .Filters.Add "Image", "*.gif; *.jpg; *.jpeg", 1
        If .Show = -1 Then

            Me.Image1.PictureSizeMode = fmPictureSizeModeZoom
            Me.Image1.Picture = LoadPicture(.SelectedItems(1))


       End If
    End With
End Sub

Open in new window

Thank you for any help you can provide.
  • 2
  • 2
1 Solution
Me.Image1.Picture = LoadPicture(.SelectedItems(1))
So here you are dynamically loading an image to Image1.
Nothing wrong with that -- but nowhere do you save that path, and nowhere do you tell the userform to load that image from a saved location when the userform first opens.

So you need to save the value of .SelectedItems(1) to a cell some place, and in the userform initialize you need to check if there's a value there and LoadPicture from it.
BunchilAuthor Commented:
Hi Nick, thanks for your help, works perfectly.
BunchilAuthor Commented:
I appreciate your very prompt response and help.
No problem,
Glad to be of service


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now