This documentation is archived and is not being maintained.

Execute Method

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.  

Begins the search for the specified file(s). Returns a Long; zero (0) if no files are found, or a positive number if one or more files are found.

expression.Execute(SortBy, SortOrder, AlwaysAccurate)

expression   Required. An expression that returns a FileSearch object.

MsoSortBy can be one of these MsoSortBy constants.
msoSortByFileName default

MsoSortOrder can be one of these MsoSortOrder constants.
msoSortOrderAscending default

AlwaysAccurate  Optional Boolean. True to have the file search include files that have been added, modified, or deleted since the file index was last updated. The default value is True.

For the command bar objects, runs the procedure or built-in command assigned to the specified command bar control. For custom controls, use the OnAction property to specify the procedure to be run.

For FileDialog objects of type msoFileDialogOpen or msoFileDialogSaveAs, carries out a user's action right after the Show method is invoked.


expression   Required. An expression that returns one of the above objects.


This example searches for all files in the My Documents folder that end with the file name extension “.doc” and then displays the location and name of each file found. The example also sorts the list of returned file names in ascending alphabetic order.

Set fs = Application.FileSearch
With fs
    .LookIn = "C:\My Documents"
    .FileName = "*.doc"
    If .Execute(SortBy:=msoSortbyFileName, _
            SortOrder:=msoSortOrderAscending) > 0 Then
        MsgBox "There were " & .FoundFiles.Count & _
            " file(s) found."
        For i = 1 To .FoundFiles.Count
            MsgBox .FoundFiles(i)
        Next i
        MsgBox "There were no files found."
    End If
End With

This Microsoft Excel example creates a command bar and then adds a built-in command bar button control to it. The button executes the Excel AutoSum function. This example uses the Execute method to total the selected range of cells when the command bar appears.

Dim cbrCustBar As CommandBar
Dim ctlAutoSum As CommandBarButton
Set cbrCustBar = CommandBars.Add("Custom")
Set ctlAutoSum = cbrCustBar.Controls _
    .Add(msoControlButton, CommandBars("Standard") _
cbrCustBar.Visible = True