VBA UserForm获取文件名 [英] VBA UserForm Get Filename

查看:218
本文介绍了VBA UserForm获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个由2个CommandButton组成的UserForm,每个CommandButton都包含一个图片。用户被要求选择两个CommandButton中的一个。我希望所选图片的文件名被复制到工作表中的单元格中。目前我无法弄清楚如何获取图片的文件名,所以我已经手动插入每个CommandButton的文件名,如下所示:

  Private Sub cmdQ2Opt1_Click()
工作表(UserQuestionnaire)。范围(C4)。Value =242.216.490
结束Sub

Private Sub cmdQ2Opt2_Click()
Worksheets(UserQuestionnaire)。Range(C4)。Value =354.129.401
End Sub

如何为VBA自动复制图片的文件名?

感谢您的帮助! 如果您在 Forms Designer 中分配图片,则路径不会存储为图片然后从工作簿本身的存储器中加载。

要坚持您在运行时加载的路径:

<$ p $ > code> dim path as string
path =C:\ ... \foo.jpg
set commandbutton1.Picture = loadpicture(path)

然后,您可以将路径存储在按钮 Tag 属性中:

  commandbutton1.Tag =路径

然后在点击时读回:

  ActiveWorkbook.Sheets(UserQuestionnaire)。Range C4)。Value = commandbutton1.Tag 


I have constructed a UserForm that consists of 2 CommandButtons, each of which contains a Picture. The user is asked to select one of the two CommandButtons. I would like the filename of the picture selected to be copied to a cell in a worksheet. At the moment I can't figure out how to get the filename of the picture and so I have manually inserted the filename for each CommandButton, as such:

Private Sub cmdQ2Opt1_Click()
    Worksheets("UserQuestionnaire").Range("C4").Value = "242.216.490"
End Sub

Private Sub cmdQ2Opt2_Click()
    Worksheets("UserQuestionnaire").Range("C4").Value = "354.129.401"
End Sub

How can I code this so that VBA automatically copies the picture's filename?

Thanks in advance for the help!

解决方案

If you assign the picture in the Forms Designer the path is not stored as the image is subsequently loaded from storage within the workbook itself.

To persist the path you would load at runtime:

dim path As string
path = "C:\...\foo.jpg"
set commandbutton1.Picture = loadpicture(path)

You can then store the path in the buttons Tag property:

commandbutton1.Tag = path

Then read it back when its clicked:

ActiveWorkbook.Sheets("UserQuestionnaire").Range("C4").Value = commandbutton1.Tag 

这篇关于VBA UserForm获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆