--- Soumis par Dev Ashish---
Repérer un fichier.
(Q) Comment chercher mon disque rigide pour trouver un fichier de nom donné? comment obtenir son répertoire?
(A) Le premier exemple est pour Access 97 seulement, pour Access 95, voir l'alternative.
On peut utiliser l'objet FileSearch d'Office 97, mais pour ce faire, s'assurer de cocher "Microsoft Office 8.0 Object Library" ( MSO97.DLL dans le répertoire d' Office ) sous Tools/References (Outils/Librairies) avant d'exécuter le code suivant.
Copier-coller le code suivant dans un nouveau module.
'******************** Code Start ************************ Function fReturnFilePath(strFilename As String, _ strDrive As String) As String Dim varItm As Variant Dim strFiles As String Dim strTmp As String If InStr(strFilename, ".") = 0 Then MsgBox "Désolé!! Le nom complet est requis", vbCritical Exit Function End If strFiles = "" With Application.FileSearch .NewSearch .LookIn = strDrive .SearchSubFolders = True .FileName = strFilename .MatchTextExactly = True .FileType = msoFileTypeAllFiles If .Execute > 0 Then For Each varItm In .FoundFiles strTmp = fGetFileName(varItm) If strFilename = strTmp Then fReturnFilePath = varItm Exit Function End If Next varItm End If End With End Function Private Function fGetFileName(strFullPath) As String Dim intPos As Integer, intLen As Integer intLen = Len(strFullPath) If intLen Then For intPos = intLen To 1 Step -1 'Repérer le dernier \ If Mid$(strFullPath, intPos, 1) = "\" Then fGetFileName = Mid$(strFullPath, intPos + 1) Exit Function End If Next intPos End If End Function
'******************** Code End **************************
Puis, à partir de votre code, appeler la fonction un peu comme suit:
strFilePath=fReturnFilePath("network.wri","C:")
(Note: Si FindFast n'est pas activé, la recherche prendra plus de temps)
Alternative: Pour Access 95 (également pour Access 97 ), vous pouvez utiliser la fonction suivante comme substitution à l'objet FileSearch.
'******************** Code Start ************************** Private Declare Function apiSearchTreeForFile Lib "ImageHlp.dll" Alias _ "SearchTreeForFile" (ByVal lpRoot As String, ByVal lpInPath _ As String, ByVal lpOutPath As String) As Long Function fSearchFile(ByVal strFilename As String, _ ByVal strSearchPath As String) As String 'Selon le permier rencontré Dim lpBuffer As String Dim lngResult As Long fSearchFile = "" lpBuffer = String$(1024, 0) lngResult = apiSearchTreeForFile(strSearchPath, strFilename, lpBuffer) If lngResult <> 0 Then If InStr(lpBuffer, vbNullChar) > 0 Then fSearchFile = Left$(lpBuffer, InStr(lpBuffer, vbNullChar) - 1) End If End If End Function
'******************** Code End **************************