Welcome to the board.
Sub ListWordCount()
'In the Visual Basic Editor,
'go to Tools -> References and check the box for
'Microsoft Scripting Runtime to access the
'filesystem object.
Dim fso As Scripting.FileSystemObject
Dim fol As Scripting.Folder
Dim cfil As Variant
Dim fil_1 As Scripting.File
'Late binding
Dim WdApp As Object
Dim WdDoc As Object
Dim s As String
Dim MyPath$
Dim Ctr As Long
Set WdApp = CreateObject("Word.Application")
'The FSO isn't the fastest object in existence
'and much slower than using the Windows API (or its
'VB.Net namesake for that matter) but it's convenient
'and easy to use.
Set fso = New FileSystemObject
MyPath$ = "C:\Users\harshadp\Desktop\New folder (2)"
Set fol = fso.GetFolder(MyPath$)
Set cfil = fol.Files
'Helps it run a bit faster...
Application.ScreenUpdating = True
Ctr = 2
For Each fil_1 In cfil
'Debug.Print fil_1.Type
Select Case fil_1.Type
'Add any other types that you want to check for
Case "Microsoft Office Word 97 - 2003 Document", "Microsoft Word Document"
Set WdDoc = WdApp.Documents.Open(Filename:=fil_1.Path, ConfirmConversions:=False, _
ReadOnly:=True)
Worksheets(1).Cells(Ctr, 1) = fil_1.Name
Worksheets(1).Cells(Ctr, 2) = WdDoc.Range.ComputeStatistics(0)
Ctr = Ctr + 1
WdDoc.Close savechanges:=False
End Select
Next
ExitPoint:
On Error Resume Next
Set fil_1 = Nothing
Set cfil = Nothing
Set fol = Nothing
Set fso = Nothing
Set WdDoc = Nothing
Set WdApp = Nothing
Application.ScreenUpdating = True
Application.ScreenRefresh
On Error GoTo 0
Exit Sub
ErrorHandler:
MsgBox Err.Number & vbCr & Err.Description
Resume ExitPoint
End Sub
Bookmarks