Hi,
I have 2 functions and a sub that search through a folder and its subfolders for jpegs matching a naming covention.
It works fine but I understand that using a filtered DIR approach would make this much quicker. The code is below.
Can anyone help?
I have 2 functions and a sub that search through a folder and its subfolders for jpegs matching a naming covention.
It works fine but I understand that using a filtered DIR approach would make this much quicker. The code is below.
Can anyone help?
Code:
Function GetFolder() As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function
Function picInsert2(folder As String, articleCode As String, material As String, colour As String, row1 As Integer, column1 As Integer)
Dim objFSO As Object
Dim objFolder, objSubfolder As Object
Dim objFile As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Enter the folder where the images are stored
Set objFolder = objFSO.GetFolder(folder) 'choose folder using GetFolder function
For Each objFile In objFolder.Files
On Error Resume Next
If objFile.Name Like ("*" & LCase(articleCode) & "*" & LCase(material) & "*" & LCase(colour) & "*jpg") Or objFile.Name Like ("*" & UCase(articleCode) & "*" & UCase(material) & "*" & UCase(colour) & "*.jpg") Then
With ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 200, 50)
.Fill.UserPicture objFile
.Left = ActiveSheet.Cells(row1, column1).Left
.Top = ActiveSheet.Cells(row1, column1).Top
.Height = ActiveSheet.Cells(row1, column1).Height
.Width = ActiveSheet.Cells(row1, column1).Width
End With
End If
Next objFile
If objFolder.Subfolders.Count > 0 Then
For Each objSubfolder In objFolder.Subfolders
Call picInsert2(objSubfolder.Path, articleCode, material, colour, row1, column1)
Next objSubfolder
End If
Set objFSO = Nothing
Set objFolder = Nothing
Set objSubfolder = Nothing
Set objFile = Nothing
End Function
Sub PrintPicFile2()
Dim i As Integer
Dim articleCode As String, colour As String, folder As String, material As String
Dim lRow As Long
Dim fdr As String
lRow = Cells(Rows.Count, 1).End(xlUp).row
fdr = GetFolder()
For i = 3 To lRow
folder = fdr
articleCode = ActiveSheet.Cells(i, 1) ' to change the column that the style name is in change the 1 = A, 2 = B ...
material = ActiveSheet.Cells(i, 2) ' to change the column that the material is in change the 1 = A, 2 = B ...
colour = ActiveSheet.Cells(i, 3) ' to change the column that the colour is in change the 1 = A, 2 = B ...
Call picInsert2(folder, articleCode, material, colour, i, 4) ' to change the column where the image goes change the 3 = C...
Rows(i).Select
Next i
End Sub