title: Search for file using VBA in MSWord
body: <p>I need my Macro to search for a specific file without knowing the full file path, then to either open the file or return the file path. I´ve tried searching online but most of the answers don't seem to work for me. The closest I got was a function to search for folders, but I wasn't able to modify it to search for files
  (courtesy of <a href="https://vbahowto.com/how-to-search-for-a-folder-in-vba/" rel="nofollow noreferrer">https://vbahowto.com/how-to-search-for-a-folder-in-vba/</a>).</p>
  <pre><code>Sub Main()
  
  Dim fsoFileSystem As Object
  Dim strMainFolder As String
  Dim strLookFor As String
  
  strLookFor = &quot;working&quot;
  strMainFolder = &quot;C:\a&quot;
  
  Set fsoFileSystem = CreateObject(&quot;Scripting.FileSystemObject&quot;)
  DoSubFolders fsoFileSystem.GetFolder(strMainFolder), strLookFor
  
  'if the code didn't find the folder, you will get this message.
  Msgbox &quot;'&quot; &amp; strLookFor &amp; &quot;' is not found so go ahead and create it.&quot;, vbInformation
  
  End Sub
  
  Sub DoSubFolders(Folder, strLookFor)
  Dim objSubFolder As Object
  
  For Each objSubFolder In Folder.SubFolders
      
      Debug.Print &quot;*****************************************&quot;
      Debug.Print &quot;SubFolder= &quot; &amp; objSubFolder.Name
      Debug.Print &quot;*****************************************&quot;
      
      If objSubFolder.Name = strLookFor Then
          MsgBox &quot;You already have a folder called '&quot; &amp; strLookFor &amp; &quot;' at '&quot; &amp; objSubFolder.Path &amp; &quot;' . Don't add it again.&quot;, vbInformation
          
          'Exit the search
          End
          
      End If
      
      DoSubFolders objSubFolder, strLookFor
  Next
  Dim objFile As Object
  For Each objFile In Folder.Files
      ' Operate on each file
      Debug.Print &quot;FileName= &quot; &amp; objFile.Name
      
  Next
  End Sub
  </code></pre>