简体   繁体   English

从用户表单(Excel VBA)向工作表添加图像

[英]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) 只需添加一个TextBox(在用户窗体中是不可见的)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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