+ Reply to Thread
Results 1 to 11 of 11

VBA to Count Words in MS Word Doc and List Down it in excel

Hybrid View

  1. #1
    Registered User
    Join Date
    06-17-2013
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2

    VBA to Count Words in MS Word Doc and List Down it in excel

    Hi Team,

    What could be done in case where i want to count words of a bunch of document in folder and also required output in excel file.

    I have something that i found from some where.

    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
    
        Dim s As String
    
        '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
        Set fol = fso.GetFolder("C:\Users\harshadp\Desktop\New folder (2)")
        Set cfil = fol.Files
    
        'Helps it run a bit faster...
        Application.ScreenUpdating = True
    
        For Each fil_1 In cfil
    
            Select Case fil_1.Type
                'Add any other types that you want to check for
                Case "Microsoft Word 97 - 2003 Document", "Microsoft Word Document"
    
                Documents.Open FileName:=fil_1.path
    
                Debug.Print fil_1.Name & vbTab & _
                 ActiveDocument.Range.ComputeStatistics(wdStatisticWords) _
                 & " words."
    
                Documents.Close savechanges:=False
    
          End Select
    
        Next
    
    ExitPoint:
    On Error Resume Next
    Set fil_1 = Nothing
    Set cfil = Nothing
    Set fol = Nothing
    Set fso = Nothing
    Application.ScreenUpdating = True
    Application.ScreenRefresh
    On Error GoTo 0
    
    Exit Sub
    
    ErrorHandler:
    
    MsgBox Err.Number & vbCr & Err.Description
    
    Resume ExitPoint
    
    End Sub
    This macro runs fine in word givign error in excel
    Last edited by harshadp; 06-18-2013 at 11:41 AM. Reason: Use code tags as per forum rule 3.

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: VBA to Count Words in MS Word Doc and List Down it in excel

    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
    David
    (*) Reputation points appreciated.

  3. #3
    Registered User
    Join Date
    06-17-2013
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: VBA to Count Words in MS Word Doc and List Down it in excel

    Thanks Tinbendr for such quick response. Its working great.

  4. #4
    Registered User
    Join Date
    07-27-2017
    Location
    India
    MS-Off Ver
    2010
    Posts
    4

    Re: VBA to Count Words in MS Word Doc and List Down it in excel

    Thanks Tinbendr.

    The path needs to provided in code, but could you please check if user can select a folder as per requirement?

    Thanks in advance.

  5. #5
    Registered User
    Join Date
    07-27-2017
    Location
    India
    MS-Off Ver
    2010
    Posts
    4

    Re: VBA to Count Words in MS Word Doc and List Down it in excel

    Thanks Tinbendr.

    If a folder contains 90 word files, then the result appears only for 60 files.

    Could you please suggest?

    Thanks in advance.

  6. #6
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: VBA to Count Words in MS Word Doc and List Down it in excel

    You really should start a new thread and reference this one.

    However, add this:
    Function GetFolder() As String
        Dim fldr As FileDialog
        Dim sItem As String
        Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
        With fldr
            .Title = "Select a Folder"
            .AllowMultiSelect = False
            .InitialFileName = Application.DefaultFilePath
            If .Show <> -1 Then GoTo NextCode
            sItem = .SelectedItems(1)
        End With
    NextCode:
        GetFolder = sItem
        Set fldr = Nothing
    End Function
    Change this
    MyPath$ = "C:\Users\harshadp\Desktop\New folder (2)"
    to
    MyPath$ = GetFolder
    Probably would be a good idea to test for a empty folder in case the user cancels.

    Sorry, but I don't really know why it's only processing 60 of 90 files.

  7. #7
    Registered User
    Join Date
    07-27-2017
    Location
    India
    MS-Off Ver
    2010
    Posts
    4

    Re: VBA to Count Words in MS Word Doc and List Down it in excel

    Thanks Tinbendr.

  8. #8
    Registered User
    Join Date
    09-14-2017
    Location
    india
    MS-Off Ver
    2013
    Posts
    2

    Re: VBA to Count Words in MS Word Doc and List Down it in excel

    Hi Tinbendr ,

    Macro working great. can i get no of pages in the word document & No of characters used in the document?

    Regards,

  9. #9
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: VBA to Count Words in MS Word Doc and List Down it in excel


  10. #10
    Registered User
    Join Date
    09-14-2017
    Location
    india
    MS-Off Ver
    2013
    Posts
    2

    Re: VBA to Count Words in MS Word Doc and List Down it in excel

    Hello friend,

    can you please give code page count...

  11. #11
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,953

    Re: VBA to Count Words in MS Word Doc and List Down it in excel

    sudarbharath,
    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    Ben Van Johnson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1