从用户表单添加图像到工作表(excel vba)

时间:2016-08-31 16:28:28

标签: excel image vba excel-vba 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)

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

所选图片存储在用户窗体的图像框中。我有一个提交按钮,当我选择时,我希望得到用户从图像框中选择的“图片”并将其插入到工作表上的特定位置。这就是我到目前为止所做的:

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

这根本不起作用,因为它会抛出错误,是否有人知道是否可以这样做?

1 个答案:

答案 0 :(得分:-1)

很简单

只需添加一个TextBox(在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