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

Excel VBA用户表单图片到工作表单元格,vba,excel,Vba,Excel,我有个问题。是否可以将工作簿用户窗体中的图片安全保存到单元格中的第二个工作簿中 “我的代码”使用名为“newsheet”的新工作表创建新工作簿。 在那里,我想在单元格值上插入某些图片到我现在所在的范围内。到目前为止,我有这样的想法: lrow = newsheet.cells(rows.count,1).end(xlup).rows for i = 1 to lrow if newsheet.range("C" & i) <> "" then 'search for

我有个问题。是否可以将工作簿用户窗体中的图片安全保存到单元格中的第二个工作簿中

“我的代码”使用名为“newsheet”的新工作表创建新工作簿。 在那里,我想在单元格值上插入某些图片到我现在所在的范围内。到目前为止,我有这样的想法:

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
lrow=newsheet.cells(rows.count,1).end(xlup).rows
对于i=1至lrow
如果newsheet.range(“C”&i)”,则
'搜索userfrom的名称,userfrom名称与单元格值相同
'并将该用户表单中的图片插入到“C”&i中
如果结束
接下来我

要将位图直接从
用户表单
复制到工作表中,没有简单的方法。工作表没有窗体所具有的
Image
对象以及添加图片时(在形状中或使用
ActiveSheet.pictures.Insert
方法),获取的参数是文件名

也就是说,您可以创建一个临时文件,将您的图片保存在UserForm中,并使用该文件在您需要的位置插入图片

我创建了一个工作簿,其中有一个名为“TestForm”的userform,上面有一个名为“Image1”的图像控件

常规模块中的以下代码实现了这一点:

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
函数,用于保存表单中的图片,前提是它位于名为“Image1”的图像控件中。还将位置返回到上面的例程:

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
删除临时文件:

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
请注意,上面的错误处理为0。如果单元格中的表单名称无效,它将崩溃。如果表单没有image类型的“Image1”控件,它将崩溃,如果将无效文件名传递给删除例程,它将崩溃


但是,它确实完成了您提到的操作:创建新工作簿,将原始工作簿中的用户表单中的图片添加到新工作簿(第1页)基于用户名。由于问题不太详细,而且您的确切用例未知,这应该足以让您启动并运行。

我认为是的。记录您执行此操作的宏,然后检查代码,并查看您要从中复制图像的对象,这是表单上的图像吗,您的问题是esn没有指定它来自表单中的哪个控件。我将图片直接插入到userform中,以便可以像:userform1.picture那样引用它。据我所知,录制宏在从单元格值引用到userform名称方面不起作用。确切地说,您可以从该宏中学习如何开始编码它不确定如何使用假设userform1是单元格c1中的字符串,然后我希望userform1中的图片显示在该单元格中。不知道如何使用record宏选项来执行此操作。您尝试过了吗?谢谢您的回答。这也很有效,但我从另一个网站启动并运行了我的代码。此代码使用API调用而不是速度运行rary文件。我不喜欢临时文件的原因是,我需要将我的宏分发给具有不同计算机和设置的许多人。因此,找到一个合适的临时路径并不容易。我现在知道我的方法没有错误处理,但不需要,因为用户只允许在单元格中插入5个不同的名称,所有这些名称有一个相应的用户表单。@webjoh这确实是另一个(可能更好)的选择-我试图坚持使用VBA中支持的方法,而不访问任何其他库。很高兴解决了这个问题。