OFF97: Microsoft Office 97 Programs FileSearch Fails on a Microsoft Windows 2000-based Computer (259738)



The information in this article applies to:

  • Microsoft Office 97 for Windows
  • Microsoft Access 97
  • Microsoft Excel 97 for Windows
  • Microsoft PowerPoint 97 for Windows
  • Microsoft Word 97 for Windows
  • the operating system: Microsoft Windows 2000

This article was previously published under Q259738

SYMPTOMS

Visual Basic code that references the Application FileSearch object may cause the following error on a computer that is running Microsoft Windows 2000 if the code includes a wildcard character in the FileName property:
Run-time error '5':

Invalid procedure call or argument

RESOLUTION

Use the Dir property to search for the files as shown in the following example. This example requires that you have .mdb files in the My Documents folder.
  1. Copy the sample database Northwind.mdb to the My Documents folder on a computer that is running Windows 2000.
  2. In Access 97, create a new database.
  3. Create a module, and then type the following line in the Declarations section if it is not already there:
    Option Explicit
    					
  4. Type the following procedure:
    Function TestDir(strPathName)
        Dim searchFile As String
        Dim resultString As String
        Dim fileCounter As Integer
        Dim messageString As String
       
        fileCounter = 0
        searchFile = Dir(strPathName)
    
        If searchFile <> "" Then
            fileCounter = 1
            resultString = searchFile & vbCrLf
        End If
    
        Do While searchFile <> ""
            searchFile = Dir
            If searchFile <> "" Then
                fileCounter = fileCounter + 1
                resultString = resultString & searchFile & vbCrLf
            End If
        Loop
    
        If fileCounter = 0 Then
            messageString = "No files found"
        Else
            messageString = "Found " & fileCounter & " file/s named:" _
                & vbCrLf & resultString
        End If
       
        TestDir = messageString
    End Function
    					
  5. To test this function, type the following line in the Debug window, and then press ENTER:
    ?TestDir("C:\My Documents\No*.mdb")
    					
    Note that the results are returned to the Debug window.

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

If you click Debug in the error message, the line of code that contains the wildcard character is highlighted. For example:
.FileName = "No*.mdb"
				
Note that the code in the "Steps to Reproduce the Behavior" section of this article works correctly the first time that you run it; however, any other attempt to run the code is not successful, and you receive the error message that is mentioned in the "Symptoms" section. If you remove the wildcard character from the code, you can run the code successfully again until running the code re-enters the wildcard character into the function.

Steps to Reproduce the Behavior

  1. Copy the sample database Northwind.mdb to the My Documents folder on a computer that is running Windows 2000.
  2. In Access 97, create a new database.
  3. Create a module, and then type the following line in the Declarations section if it is not already there:
    Option Explicit
    					
  4. On the Tools menu, click References, and then click to select the Microsoft Office 8.0 Object Library check box.
  5. Type the following procedure:
    Function TestSearch(strLookIn, strLookFor) 
        Dim intCount As Integer 
    
        intCount = 0
    
        With Application.FileSearch 
            .NewSearch 
            .FileName = strLookFor
            .LookIn = strLookIn 
            .SearchSubFolders = False 
            .MatchTextExactly = True 
            .FileType = msoFileTypeAllFiles 
            If .Execute > 0 Then 
                intCount = .FoundFiles.Count 
            End If 
        End With
    
        testSearch = "Files found = " & intCount
    End Function
    					
  6. To test this function, type the following line in the Debug window, and then press ENTER:
    ?testSearch("c:\My Documents\", "No*.mdb")
    					
    Note that the results are returned to the Debug window the first time that you run the code.

    If you run the code a second time, you may receive the error message mentioned in the "Symptoms" section.

REFERENCES

For more information about the Dir function, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type dir in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Modification Type:MajorLast Reviewed:5/23/2003
Keywords:kbbug kbdta kbnofix KB259738