VBA RegEx to find files
<p>I need to find files in a folder, and I have 3 cases on files naming:</p>

<ol>
<li>DI0425522.pdf </li>
<li>AL-DN-DI0425523.pdf</li>
<li>AL-DN-DI0425524-2016-11-17_1108.pdf</li>
</ol>

<p>I can handle the first and the second case, but I need to find the third too. the last 16 characters of the 3. filename can variate, so I think to use RegExp to match it, then copy all files in another folder. </p>

<p>the string is stored in an excel cell, but only with "DI#######" naming</p>

<ol>
<li>DI0425522 (A2 cell)</li>
<li>DI0425523 (A3 cell)</li>
<li>DI0425524 (A4 cell)</li>
</ol>

<p>This is the code, but it doesn't work: it shows error 438 "object doesn't support this property or method" on the <code>If Dir(Source &amp; "\DI\" &amp; "AL-DN-" &amp; ValoreCella &amp; regex &amp; ".Pdf") &lt;&gt; "" Then</code> line</p>

<pre><code>Sub cerca()
Dim T As Variant
Dim D As Variant

T = VBA.Format(VBA.Time, "hh.mm.ss")
D = VBA.Format(VBA.Date, "yyyy.MM.dd")

Dim Ricercatore As Variant

Ricercatore = Cells(1, 3)

Dim Source As String
Dim Dest As String

Source = "\\it-s-bolo02\WORKGROUP\Comune\000_0_______ COMUNE 2011\15_TECNICO\AAA - RICERCA DDT\ALSS\DDT"
Dest = "\\it-s-bolo02\WORKGROUP\Comune\000_0_______ COMUNE 2011\15_TECNICO\AAA - RICERCA DDT\ALSS\Ricerca\Ricerca " &amp; D &amp; " " &amp; T &amp; " " &amp; Ricercatore

MkDir Dest

Dim ValoreCella As Variant, r As Long, DDTmancanti As Variant

r = 2

Do Until Cells(r, 1) = ""

ValoreCella = Cells(r, 1)

    If Dir(Source &amp; "\DI\" &amp; ValoreCella &amp; ".Pdf") &lt;&gt; "" Then
        FileCopy Source &amp; "\DI\" &amp; ValoreCella &amp; ".Pdf", Dest &amp; "\" &amp;   ValoreCella &amp; ".Pdf"
    Else

        If Dir(Source &amp; "\DI\" &amp; "AL-DN-" &amp; ValoreCella &amp; ".Pdf") &lt;&gt; "" Then
            FileCopy Source &amp; "\DI\" &amp; "AL-DN-" &amp; ValoreCella &amp; ".Pdf", Dest &amp; "\" &amp; "AL-DN-" &amp; ValoreCella &amp; ".Pdf"
        Else

            Dim regex As Object, str As String

            Set regex = CreateObject("VBScript.RegExp")
            str = "-([0-9]*)-([0-9]*)-([0-9]*)_([0-9]*)"
                With regex
                  .Pattern = str
                  .Global = True
                End With

            If Dir(Source &amp; "\DI\" &amp; "AL-DN-" &amp; ValoreCella &amp; regex &amp; ".Pdf") &lt;&gt; "" Then
                FileCopy Source &amp; "\DI\" &amp; "AL-DN-" &amp; ValoreCella &amp; regex &amp; ".Pdf", Dest &amp; "\" &amp; "AL-DN-" &amp; ValoreCella &amp; regex &amp; ".Pdf"
            Else

                If Dir(Source &amp; "\Altro\" &amp; ValoreCella &amp; ".Pdf") &lt;&gt; "" Then
                    FileCopy Source &amp; "\Altro\" &amp; ValoreCella &amp; ".Pdf", Dest &amp; "\" &amp; ValoreCella &amp; ".Pdf"
                Else
                    DDTmancanti = DDTmancanti &amp; ValoreCella &amp; vbCrLf
                End If

            End If

        End If

    End If

r = r + 1

Loop

Dim FF As Long
FF = FreeFile

Open (Dest &amp; "\" &amp; "0 - DDT_mancanti.txt") For Output As #FF
Write #FF, DDTmancanti
Close #FF

MsgBox "Operazione eseguita"
Shell "explorer.exe " + Dest, vbNormalFocus

End Sub
</code></pre>

<p>Thanks for help</p>