[英]Adding image to a worksheet from a userform (excel vba)
我有一個帶有瀏覽按鈕的用戶窗體,該按鈕允許用戶搜索驅動器並選擇圖片(例如徽標):
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
這根本不起作用,因為它會引發錯誤,有人知道這是否可以完成嗎?
這很簡單
只需添加一個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.