简体   繁体   English

Excel VBA 用户表单图片到工作表单元格

[英]Excel VBA Userform Picture to worksheet cell

I have a question.我有个问题。 Is it possible to safe a picure from a workbook userform to a second workbook into a cell.是否可以将工作簿用户表单中的图片安全地保存到单元格中的第二个工作簿中。

My code creates a new workbook with a new sheet named newsheet.我的代码使用名为 newsheet 的新工作表创建了一个新工作簿。 There I want to insert certain pictures on cell value into the range I am in right now.在那里,我想将单元格值上的某些图片插入到我现在所在的范围中。 So far I have something like this:到目前为止,我有这样的事情:

lrow = newsheet.cells(rows.count,1).end(xlup).rows
for i = 1 to lrow
 if newsheet.range("C" & i) <> "" then 
   'search for name of userfrom, the userfrom name is the same as cell value
     'and insert that picture from that userform into "C" & i
  end if
  next i

There's no easy way to copy the bitmap straight from the UserForm to the Worksheet.没有简单的方法可以将位图直接从UserForm复制到工作表。 Worksheet doesn't have Image objects the way the form has and when adding pictures (either in a Shape, or using ActiveSheet.Pictures.Insert method, the parameter taken is a filename. Worksheet 没有像表单那样具有Image对象,并且在添加图片时(在 Shape 中,或使用ActiveSheet.Pictures.Insert方法,采用的参数是文件名。

That said, you can create a temporary file to save the picture you have in the UserForm and use that file to insert the picture at the location you need.也就是说,您可以创建一个临时文件来保存用户窗体中的图片,并使用该文件将图片插入到您需要的位置。

I created a workbook that has a userform named "TestForm" with one Image control named "Image1" on it.我创建了一个工作簿,它有一个名为“TestForm”的用户窗体,上面有一个名为“Image1”的图像控件。

The following code in a regular module does the trick:常规模块中的以下代码可以解决问题:

Sub Test()
Dim wb As Workbook
Dim ws As Worksheet
Dim formname As String
Dim tempfile As String

'Create new workbook:
Set wb = Workbooks.Add
Set ws = wb.Sheets(1)

'Setting form name in new sheet. Using row 1 in this example.
ws.Range("C1").Value = "TestForm"

'Retrieve the "found" value
formname = ws.Range("C1").Value

'Save the picture and get the location:
tempfile = SavePictureFromForm(formname)

'Navigate to the correct location, since we need it selected for Pictures.Insert
ws.Activate
Range("C1").Select
'Add the picture to the sheet:
ActiveSheet.Pictures.Insert tempfile

'Clean up the file system:
DeleteTempPicture tempfile
End Sub

Function that saves the picture from the form, provided it's in an Image control named "Image1".保存窗体中图片的函数,前提是它位于名为“Image1”的 Image 控件中。 Also returns the location to the routine above:还将位置返回到上面的例程:

Function SavePictureFromForm(formname As String) As String
Dim tempfilepath As String
Dim tempfilename As String

'Location + filename:
tempfilepath = "C:\Temp\"
tempfilename = "temppicture.jpg"

'Get the correct userform:
Set Obj = VBA.UserForms.Add(formname)

'Save the picture and return it's location:
SavePicture Obj.Image1.Picture, tempfilepath & tempfilename
SavePictureFromForm = tempfilepath & tempfilename

End Function

Delete the temporary file:删除临时文件:

Public Sub DeleteTempPicture(filename As String)
'Delete the temporary file throught FSO:
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
With FSO
    .DeleteFile filename
End With
Set FSO = Nothing
End Sub

Note that the above has ZERO error handling.请注意,上面有零错误处理。 If the Name of the form in the cell is invalid, it'll crash.如果单元格中表单的名称无效,它将崩溃。 If the form doesn't have an "Image1" control of type image, it'll crash, if you pass an invalid filename to the deletion routine, it'll crash.如果表单没有图像类型的“Image1”控件,它将崩溃,如果您将无效的文件名传递给删除例程,它将崩溃。

However - it does do what you mentioned: Create new workbook, add the picture from a userform in the original workbook to the new workbook (on sheet 1) based on the userform name.但是 - 它确实执行您提到的操作:创建新工作簿,根据用户表单名称将原始工作簿中用户表单中的图片添加到新工作簿(在工作表 1 上)。 Since the question isn't more detailed and your exact use case is unknown, this should be more than sufficient to get you up and running.由于问题不是更详细,并且您的确切用例未知,因此这应该足以让您启动并运行。

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

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