Article ID: 170431
Article Last Modified on 11/23/2006
Application.ScreenUpdating = FalseIf you convert a large number of files, this line may cause the macro to appear to stop responding.
Application.ScreenUpdating = False
If you convert a large number of files, this line may cause the macro to
appear to stop responding.
The following sample Visual Basic for Applications macro converts the files
in a folder and its subfolders:
Sub Converter()
'Turns off alerts, allows duplicate files to be overwritten without
prompts
Application.DisplayAlerts = False
'Initializes conversion conditions.
SearchDir = InputBox("Input name of directory to start in")
FileExt = InputBox("Input extension of files to convert in the form
*.xxx")
SaveDir = InputBox("Input name of directory to save files to")
SearchSubs = MsgBox(prompt:="Do you want to include files in
subdirectories?", Buttons:=vbYesNo)
'Initializes the search.
With Application.FileSearch
.NewSearch
.LookIn = SearchDir
'Determines whether to search subdirectories.
If SearchSubs = 6 Then
.SearchSubFolders = True
Else
.SearchSubFolders = False
End If
'Determines file type to convert.
.FileName = FileExt
.MatchTextExactly = True
'If the search found files, loops again, opens, and then resaves in the
'Microsoft Excel workbook format
If .Execute > 0 Then
counter = 0
For i = 1 To .FoundFiles.Count
counter = counter + 1
Workbooks.Open FileName:=.FoundFiles(i)
'Builds the new name by deleting the previous extension.
ActiveWorkbook.SaveAs FileName:=SaveDir & "\" & _
Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4), _
FileFormat:=xlWorkbookNormal
ActiveWorkbook.Close savechanges:=False
Next i
'Returns how many files were converted.
MsgBox prompt:=counter & " files were converted"
ElseIf .Execute = 0 then
MsgBox "No files were found, no files were converted"
End If
End With
End Sub
120802 Office: How to Add/Remove a Single Office Program or Component
Additional query words: XL97
Keywords: kbdtacode kbhowto KB170431