2016-08-31 139 views
1

我有一个浏览按钮,使用户能够通过他们的驱动器搜索并选择一张图片(例如标志)一个窗体:从一个窗体(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 

这不工作,因为它抛出一个错误可言,没有人知道是否可以做到这一点?

+0

在编程中,“它抛出一个错误”并不是一个非常有用的问题说明。 *发生了什么错误*,以及代码的哪一行? –

+0

你已经有图像文件的路径和名称(来自用户输入),为什么不使用'ThisWorkbook.Worksheets(3).Pictures.Insert(“File Path Here”)? – Kyle

+0

你考虑过使用'SavePicture Me.ControlName.Picture,Filename',然后'activesheet.pictures.insert(filename)'? –

回答

-1

很简单

只是增加了一个文本框(可在用户窗体不可见的)

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 
相关问题