我有一张带有一些产品ID代码的Excel工作表
100-10R 23P901 ......
我有一个存储产品图像的文件夹。图片的开头有产品代码,但结尾可能不同。
是否有人知道是否可以根据产品代码在外部文件夹中输出图片?
答案 0 :(得分:0)
您需要查看FileSystemObject,因为这可以让您查看与FileSystem有关的内容。
我已经做了一个简单的例子,虽然它可能没有像你想做的那样做,因为我不完全确定。我希望它很容易理解。
以下代码(如果放在模块中)将允许您访问工作表中的“checkList”功能。
checkList函数将遍历指定文件夹中的缓存文件名列表,检查参数“Name”是否与列表中存储的任何项匹配。
缓存的文件列表(FileList集合)将填充一次,第一次由loadList子句调用checkList。
loadList子读取指定的文件夹,并将所有文件名添加到集合中。
' Global Variable, used to cache the filenames
Public FileList As Collection
' this function returns "" if no match was found, and returns the file name if found.
Public Function checkList(ByVal Name As String) As String
Dim Result As String
Dim Item As Variant
Result = ""
If FileList Is Nothing Then
loadList
End If
For Each Item In FileList
' Performs a simple match on the filename, probably needs to be adjusted!
If Item Like Name & "*" Then
Result = Item
End If
Next Item
checkList = Result
End Function
' populates the FileList collection
Public Sub loadList()
Dim FSO As Object
Dim Folder As Object
Dim File As Object
If FileList Is Nothing Then
' First Run, needs to be created
Set FileList = New Collection
Else
' Should not happen unless you call it manually.
Set FileList = Nothing ' Clear up the old list
Set FileList = New Collection ' Create a new empty one
End If
Set FSO = CreateObject("Scripting.FileSystemObject")
' Change "C:\" to the location of the folder you want to search
Set Folder = FSO.GetFolder("C:\")
For Each File In Folder.Files
' Add the name to the FileList
FileList.Add File.Name
Next
End Sub