macro to match part of PDF file name in cell with folders & subfolders to open it

Ali M

Active Member
Joined
Oct 10, 2021
Messages
330
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hi guys
I search for macro to match part of file name in cells for column C across sheets with file names in folders & subfolders
the PDF files names will be like this in column C for each sheet
REFERENCE OPERATION NUMBER TK_12346757
DEL PROCESS REF HJ123345
CHECK VERSUS GT-1234556

the PDF files names will be TK_12346757,HJ123345,GT-1234556 in folders & subfolders .
so when match the part of PDF file name in column C for each sheet with file name in folders & subfolders should be based on the last item because the last item is matched . then should hyperlink the cells in column C for each sheet and open it when click cell , if the last item is not matched when match the part of PDF file name in column C for each sheet with file name in folders & subfolders should pops message "the file is not found ".
the files will be in folders & subfolders like this
C:\Users\RAMDANI\Desktop\Folder\PRT
C:\Users\RAMDANI\Desktop\Folder\PRT\output\
C:\Users\RAMDANI\Desktop\Folder\PRT\output\operation\
thanks in advance
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi John
I 'm not sure if I can modify your code . it's really difficult for me , but will show user defined type not defined when I test your code for this
Private Sub Get_Files_In_Folder(folderPath As String, filesArrayList As mscorlib.ArrayList)
 
Upvote 0
but will show user defined type not defined when I test your code for this
Private Sub Get_Files_In_Folder(folderPath As String, filesArrayList As mscorlib.ArrayList)
As stated in my post on the other thread, you must set a reference to mscorlib.dll, otherwise you'll get that error. Do this via Tools -> References in the VBA editor and tick mscorlib.dll in the list of Available References. If it isn't listed, click the Browse... button and find mscorlib.tlb in a subfolder of C:\Windows\Microsoft.NET\Framework\. On my machine mscorlib.tlb is in the \v4.0.30319\ subfolder, so the full path is C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.tlb.

Note that the early binding reference to mscorlib.dll is needed because of the Implements mscorlib.IComparer line in the clsFileComparer class module. Without that class module the reference isn't needed and we could do this in the main procedure:

VBA Code:
    Dim filesArrayList As Object
    Set filesArrayList = CreateObject("System.Collections.ArrayList")
instead of:

VBA Code:
    Dim filesArrayList As mscorlib.ArrayList
    Set filesArrayList = New mscorlib.ArrayList
However, clsFileComparer is needed for the binary search of the ArrayList.

Here is the code modified for your situation.

Standard module
VBA Code:
'References required
'mscorlib.dll (C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.tlb)

Option Explicit

Public Sub Find_Files_Create_Hyperlinks()

    Dim mainFolder As String
    Dim ws As Worksheet
    Dim Ccell As Range
    Dim filesArrayList As mscorlib.ArrayList
    Dim fileFoundIndex As Long
    Dim file As clsFile
    Dim fileComparer As clsFileComparer
   
    mainFolder = "C:\Users\RAMDANI\Desktop\Folder\PRT\"
   
    'Get listing of all files in main folder and its subfolders and put into an ArrayList.
    'Each ArrayList item is a clsFile object which has separate properties for the file name and folder path, so that just the file name part can be searched
   
    Set filesArrayList = New mscorlib.ArrayList
    Get_Files_In_Folder mainFolder, filesArrayList
       
    'Sort the ArrayList in ascending order of file name, for fast searching using BinarySearch_3 method below

    Set fileComparer = New clsFileComparer
    filesArrayList.Sort_2 fileComparer

    'Loop all sheets

    For Each ws In ThisWorkbook.Worksheets
       
        'Loop column C cells starting at C2
       
        For Each Ccell In ws.Range("C2", ws.Cells(ws.Rows.Count, "C").End(xlUp))
       
            'Put last part of this cell's value (the file name being sought) in a clsFile structure and search for it in filesArrayList using BinarySearch
           
            Set file = New clsFile
            file.fileName = Mid(Ccell.Value, InStrRev(Ccell.Value, " ") + 1) & ".pdf"
            file.folderPath = ""
            Ccell.Hyperlinks.Delete
           
            fileFoundIndex = filesArrayList.BinarySearch_3(file, fileComparer)
           
            If fileFoundIndex >= 0 Then
                Set file = filesArrayList(fileFoundIndex)
                Ccell.Worksheet.Hyperlinks.Add Anchor:=Ccell, Address:=file.folderPath & file.fileName, TextToDisplay:=Ccell.Value
            Else
                'File not found
                If MsgBox("File not found." & vbCrLf & vbCrLf & _
                       "Sheet: " & ws.Name & ", cell: " & Ccell.Address(False, False) & vbCrLf & _
                       "File: " & Mid(Ccell.Value, InStrRev(Ccell.Value, " ") + 1) & ".pdf" & vbCrLf & vbCrLf & _
                       "Click OK to continue or Cancel to quit.", vbExclamation + vbOKCancel) = vbCancel Then Exit Sub
            End If
           
        Next
       
    Next
      
End Sub


Private Sub Get_Files_In_Folder(folderPath As String, filesArrayList As mscorlib.ArrayList)

    Dim WSh As Object   'WshShell
    Dim command As String
    Dim files As Variant
    Dim file As clsFile
    Dim i As Long, p As Long
   
    Set WSh = CreateObject("WScript.Shell") 'New WshShell

    'Get list of all files in specified folder and its subfolders
   
    command = "cmd /c DIR /S /B " & Chr(34) & folderPath & Chr(34)
    files = Split(WSh.Exec(command).StdOut.ReadAll, vbCrLf)
   
    'Put the files in an ArrayList.  Each file item is a clsFile object
   
    For i = 0 To UBound(files) - 1
        p = InStrRev(files(i), "\")
        Set file = New clsFile
        file.fileName = Mid(files(i), p + 1)
        file.folderPath = Left(files(i), p)
        filesArrayList.Add file
    Next
   
End Sub

Class module, renamed as clsFile
VBA Code:
Option Explicit

Public fileName As String
Public folderPath As String

Class module, renamed as clsFileComparer
VBA Code:
Option Explicit

Implements mscorlib.IComparer

'Compare the fileName string property of two clsFile objects
'Returns:
'   -1      file1.fileName is less than file2.fileName
'   0       file1.fileName is equal to file2.fileName
'   1       file1.fileName is greater than file2.fileName

Private Function IComparer_Compare(ByVal file1 As Variant, ByVal file2 As Variant) As Long
    'Debug.Print file1.fileName, file2.fileName
    IComparer_Compare = StrComp(file1.fileName, file2.fileName, vbTextCompare)
End Function

Private Sub IComparer_TypeCheck(ByVal CurrentVarType As VbVarType, Example As Variant)
    'This second interface callback routine is not used
End Sub
 
Last edited:
Upvote 0
s stated in my post on the other thread, you must set a reference to mscorlib.dll, otherwise you'll get that error
sorry I don't read well
gives automation error in this line
VBA Code:
Set filesArrayList = CreateObject("System.Collections.ArrayList")
 
Upvote 0
gives automation error in this line
VBA Code:
Set filesArrayList = CreateObject("System.Collections.ArrayList")
No, don't use that. I only posted that to explain why you need to set the reference and that
VBA Code:
    Dim filesArrayList As mscorlib.ArrayList
    Set filesArrayList = New mscorlib.ArrayList
is needed, as shown in the main procedure.
 
Upvote 0
the same error in this line
VBA Code:
 Set filesArrayList = New mscorlib.ArrayList
 
Upvote 0
yes and I made sure again . I see it to be selected.
 
Upvote 0
Is it ticked? Like this:
1691270915306.png


Note - I browsed to and selected C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.tlb and VBA automatically changed the location to C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.tlb (I'm using 64-bit Excel).
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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
Back
Top