VBA to attach image to Excel file based on common string/text

reddsable

New Member
Joined
Jun 3, 2019
Messages
12
Hi everyone, I have the below code that adds a new sheet to all of my files from subfolders. it works as expected, except when I call a function that I created.
VBA Code:
Sub MasterMac()

    Dim MyFolder As String 
    Dim myFile As String n
    Dim wbk As Workbook 
    Dim FSO As New FileSystemObject 
    Dim ParentFolder As Object, Sub_Folder As Object
    Dim FldrPicker As FileDialog

With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Please select a folder"
        .Show
        .AllowMultiSelect = False

        If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
            MsgBox "You did not select a folder"
            Exit Sub
        End If
        MyFolder = .SelectedItems(1) & "\" 
    End With

For Each Sub_Folder In FSO.GetFolder(MyFolder).SubFolders
        myFile = Dir(MyFolder & Sub_Folder.Name & "\") 
        
        Do While myFile <> ""

        If myFile Like "*ABCD*" Then
            
            Set wbk = Workbooks.Open(Filename:=MyFolder & Sub_Folder.Name & "\" & myFile)
            With wbk
            Sheets.Add.Name = "NewSheetABCD25"
            End With

            Call PicInsert
            
            wbk.Close savechanges:=True
        End If
        myFile = Dir 
    Loop
Next Sub_Folder

End Sub

My idea with the function is to pick a folder where some images are saved, and compare them with the name of my workbooks in subfolders. If inside the name of the image is a text that can be found also in the name of the Excel file, I would like to attach that image to that excel file. That is basically my condition. But I can't seem to get a working code. This is what I have so far:

VBA Code:
Public Function PicInsert(Optional row As Integer, Optional column As Integer)

Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
Dim ws As Worksheet
Dim NewFolder As String
Dim myFile As String



With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Please select a folder"
        .Show
        .AllowMultiSelect = False

        If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
            MsgBox "You did not select a folder"
            Exit Function
        End If
        NewFolder = .SelectedItems(1) & "\" 
    End With

    myFile = Dir(NewFolder & "\" & "*.jp*")
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(NewFolder)


    
    i = 1
For Each objFile In objFolder.Files

    If InStr(1, objFile.Name, "jpg", vbTextCompare) Like "*ABD*" _
    And InStr(1, objFile.Name, "jpg", vbTextCompare) Like ActiveWorkbook.Name Then 
        With ActiveSheet.Pictures.Insert(objFile.path)
            With .ShapeRange
                .LockAspectRatio = msoTrue
                .Width = 5
                .Height = 15
            End With
            .Left = ActiveSheet.Cells(row, column).Left
            .Top = ActiveSheet.Cells(row, column).Top
            .Placement = 1 'locks the picture to a cell
        End With
    End If
    i = i + 1
Next objFile
myFile = Dir
End Function

Can anyone help?
 

Some videos you may like

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Watch MrExcel Video

Forum statistics

Threads
1,127,828
Messages
5,627,135
Members
416,223
Latest member
RichardHell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top