Colección FileTypes

Microsoft Office Objects

Colección FileTypes

         
FileSearch FileTypes

Una colección de valores del tipo msoFileType que determina qué tipos de archivos se devuelven con el método Execute del objeto FileSearch.

Utilizar la colección FileTypes

Utilice la propiedad FileTypes con el objeto FileSearch para devolver una colección FileTypes, por ejemplo:

Set ft = Application.FileSearch.FileTypes

Nota    La propiedad FileType del objeto FileSearch borra la colección FileTypes y establece el primer elemento de la colección al tipo de archivo que defina la propiedad FileType.

Sólo existe una colección FileTypes para todas las búsquedas, por lo que es importante borrar la colección FileTypes antes de realizar una búsqueda, a menos que desee buscar los tipos de archivos de búsquedas anteriores. La forma más sencilla de borrar la colección es establecer la propiedad FileType para el primer tipo de archivo que desee buscar. También puede quitar tipos individuales mediante el método Remove. Para determinar el tipo de archivo de cada elemento de la colección, utilice el método Item a fin de devolver el valor msoFileType.

El ejemplo siguiente busca todos los archivos de Microsoft Excel y HTML de la unidad C:\.

Sub SearchForFiles()

    'Declare a variable to act as a generic counter
    Dim lngCount As Long

    'Use a With...End With block to reference the
    'FileSearch object
    With Application.FileSearch

        'Clear all the parameters of the previous searches.
        'This method doesn't clear the LookIn property or
        'the SearchFolders collection.
        .NewSearch

        'Setting the FileType property clears the
        'FileTypes collection and sets the first
        'item in the collection to the file type
        'defined by the FileType property.
        .FileType = msoFileTypeWebPages

        'Add a second item to the FileTypes collection
        .FileTypes.Add msoFileTypeExcelWorkbooks

        'Display the number of FileTypes in the collection.
        MsgBox "You are about to search for " & .FileTypes.Count & _
            " file types."

        'Set up the search to look in all subfolders on the C:\ drive.
        .LookIn = "C:\"
        .SearchSubFolders = True

        'Execute the search and test to see if any files
        'were found.
        If .Execute <> 0 Then

            'Display the number of files found.
            MsgBox "Files found: " & .FoundFiles.Count

            'Loop through the list of found files and
            'display the path of each one in a message box.
            For lngCount = 1 To .FoundFiles.Count
                If MsgBox(.FoundFiles.Item(lngCount), vbOKCancel, _
                    "Found files") = vbCancel Then

                    'Break out of the loop
                    lngCount = .FoundFiles.Count

                End If
            Next lngCount
        Else
            MsgBox "No files found."
        End If
    End With
End Sub

El ejemplo siguiente recorre la colección FileTypes y quita los tipos de archivo que no sean de Microsoft Word o Microsoft Excel (en general, es más sencillo borrar la colección FileTypes y empezar de cero).

Sub RemoveFileTypeFromCollection()

    'Define an integer to use as a counter
    'when iterating through the FileTypes collection.
    Dim intFileIndex As Integer

    'Use a With...End With block to reference the FileSearch object.
    With Application.FileSearch

        'Loop through all of the items in the FileTypes collection.
        intFileIndex = 1
        Do While intFileIndex <= .FileTypes.Count
            Select Case .FileTypes.Item(intFileIndex)
                Case msoFileTypeWordDocuments, msoFileTypeExcelWorkbooks
                Case Else

                    'If the file type isn't a Microsoft Word or
                    'Microsoft Excel file, remove it.
                    .FileTypes.Remove intFileIndex

                    'Decrement the counter so that no file types are missed.
                    intFileIndex = intFileIndex - 1
            End Select

            'Increment the counter to test the next file type.
            intFileIndex = intFileIndex + 1
        Loop

    End With
End Sub