简体   繁体   中英

Adding image to a worksheet from a userform (excel vba)

I have a userform with a browse button that allows users to search through their drive and select a picture (for example a logo):

Private Sub BrowseButton_Click()

Dim strFileName As String

'use GetOpenFilename Method to select picture
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
    'load picture to Image control, using LoadPicture property
    Me.Image1.Picture = LoadPicture(strFileName)
    'after any change vba has to be told to refresh the UserForm for the change to appear
    Me.Repaint
    'label caption changes after picture is loaded
    Me.Label1.Caption = "Logo loaded"
End If

End Sub

The selected picture is stored in an image box on the userform. I have a submit button that, when selected, I wish to get the "picture" that the user selected from the image box and insert it in a specific location on a sheet. This is what I have so far:

Sub Image9_Click()

'' Submit Button

Dim sld As Worksheet
Set sld = Sheets("Sliders")

Dim logo As Image
logo = colourForm.Image1

Call updateAllColScheme ''Ignore this

colourForm.Hide

End Sub

This doesn't work at all as it throws an error, does anyone know if this can be done?

It's simple

just adds a TextBox (can be invisible in userform)

Private Sub BrowseButton_Click()

Dim strFileName As String

use GetOpenFilename Method to select picture
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)

TextBox1 = strFileName  'use to save URL or Link from picture

If strFileName = "False" Then
    MsgBox "File Not Selected!"
    Else
    'load picture to Image control, using LoadPicture property
    Me.Image1.Picture = LoadPicture(strFileName)
    'after any change vba has to be told to refresh the UserForm for the change to appear
    Me.Repaint
    'label caption changes after picture is loaded
    Me.Label1.Caption = "Logo loaded"
End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM