Results 1 to 10 of 10

Thread: [RESOLVED] microsoft word VBA coding that counts word/s in the document

  1. #1

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Resolved [RESOLVED] microsoft word VBA coding that counts word/s in the document

    I need a code that counts word/s in the microsoft word document. Can it be done using MS VB 6.0? I am neophyte to this forum, thanks in advance.

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: microsoft word VBA coding that counts word/s in the document

    VB Code:
    1. Sub countwords()
    2. '
    3. ' countwords Macro
    4. ' Macro created 16/11/2005 by
    5. '
    6.     Debug.Print ActiveDocument.Words.Count
    7. End Sub
    This world is not my home. I'm just passing through.

  3. #3

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Re: microsoft word VBA coding that counts word/s in the document

    sorry if i did not explain briefly my question in the first post... here it is, i need a code that count specific word or words in the microsoft word document, i mean the occurrence of the word or words in the document. i need this to be coded in the visual basic 6.0, how can it be done? thanks in advance.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: microsoft word VBA coding that counts word/s in the document

    Use the .Find method in a loop counting the instances.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Re: microsoft word VBA coding that counts word/s in the document

    sir RobDog888, could you please show me a code on this. it would be helpful for me if i can see the code. sorry for the ignorant. thanks for helping me. hoping for your kind and consideration.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: microsoft word VBA coding that counts word/s in the document

    Sure no problem. Here is an example from the helpfile.
    VB Code:
    1. With ActiveDocument.Content.Find
    2.     .ClearFormatting
    3.     .Style = wdStyleHeading3
    4.     Do While .Execute(FindText:="", Forward:=True, _
    5.             Format:=True) = True
    6.         With .Parent
    7.             .StartOf Unit:=wdParagraph, Extend:=wdMove
    8.             .InsertAfter "Tip: "
    9.             .Move Unit:=wdParagraph, Count:=1
    10.         End With
    11.     Loop
    12. End With
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Lively Member neo_phyte's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Philippines
    Posts
    71

    Re: microsoft word VBA coding that counts word/s in the document

    it seems that you give me a code using the VBA coding not the VB 6.0 coding.... is that possible if i code that using VB 6.0, does it run... There are methods in VBA that can not be supported in VB 6.0, correct me if i am wrong....

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: microsoft word VBA coding that counts word/s in the document

    VBA is a subset of VB, so nearly all VBA code can be used in VB. Also, all the methods that RobDog used are methods of the Word object model, rather than anything VBA specific. When you create an instance of a Word object in VB, you can use it in the same way as you would in VBA (so you can almost copy & paste macro's into VB programs).

    In the code above all you need to change is "ActiveDocument", which needs to be the Document object variable that you have created & initialised. From your comments I assume you don't know how to do that, so here's a little example:
    VB Code:
    1. 'Add a reference (under "Project"->"References") to "Microsoft Word [i]X.X[/i] object library"
    2.  
    3. 'declare objects
    4. Dim oWordApp as Word.Application
    5. Dim oWordDoc as Word.Document
    6. 'set up objects
    7.   Set oWordApp = New Word.Application
    8.   Set oWordDoc = oWordApp.Documents.Open("c:\my folder\test.doc")
    9.  
    10. 'work with objects   (just a copy of the post above)
    11. With [b]oWordDoc[/b].Content.Find
    12.     .ClearFormatting
    13.     .Style = wdStyleHeading3
    14.     Do While .Execute(FindText:="", Forward:=True, _
    15.             Format:=True) = True
    16.         With .Parent
    17.             .StartOf Unit:=wdParagraph, Extend:=wdMove
    18.             .InsertAfter "Tip: "
    19.             .Move Unit:=wdParagraph, Count:=1
    20.         End With
    21.     Loop
    22. End With
    23.  
    24. 'save & close objects
    25.   oWordDoc.Save
    26.   oWordDoc.Close
    27.   Set oWordDoc = Nothing
    28.   oWordApp.quit
    29.   Set oWordApp = Nothing
    (note: there may be minor errors, as I just typed this into the browser!)

  9. #9
    Junior Member
    Join Date
    Nov 2005
    Posts
    29

    Re: microsoft word VBA coding that counts word/s in the document

    to find the details of document
    iPg = 0: iPg = wdoc.ComputeStatistics(wdStatisticPages)
    iWrd = 0: iWrd = wdoc.ComputeStatistics(wdStatisticWords)
    iChrNoSp = 0: iChrNoSp = wdoc.ComputeStatistics(wdStatisticCharacters)
    iChrWSp = 0: iChrWSp = wdoc.ComputeStatistics(wdStatisticCharactersWithSpaces)
    iPara = 0: iPara = wdoc.ComputeStatistics(wdStatisticParagraphs)
    iLines = 0: iLines = wdoc.ComputeStatistics(wdStatisticLines)

    Header and FOtter Count
    Set wFRangeHdr = wPageStart.Sections(1).Headers(wdHeaderFooterFirstPage).Range
    Set wFRangeFtr = wPageStart.Sections(1).Footers(wdHeaderFooterFirstPage).Range
    Set wPRangeHdr = wPageStart.Sections(1).Headers(wdHeaderFooterPrimary).Range
    Set wPRangeFtr = wPageStart.Sections(1).Footers(wdHeaderFooterPrimary).Range
    If (iSection < wSec.Index) And wSec.PageSetup.DifferentFirstPageHeaderFooter Then

    HdrWrd = wFRangeHdr.ComputeStatistics(wdStatisticWords)
    HdrChrNoSp = wFRangeHdr.ComputeStatistics(wdStatisticCharacters)
    HdrChrWsp = wFRangeHdr.ComputeStatistics(wdStatisticCharactersWithSpaces)
    HdrPara = wFRangeHdr.ComputeStatistics(wdStatisticParagraphs)
    HdrLines = wFRangeHdr.ComputeStatistics(wdStatisticLines)

    FtrWrd = wFRangeFtr.ComputeStatistics(wdStatisticWords)
    FtrChrNoSp = wFRangeFtr.ComputeStatistics(wdStatisticCharacters)
    FtrChrWsp = wFRangeFtr.ComputeStatistics(wdStatisticCharactersWithSpaces)
    FtrPara = wFRangeFtr.ComputeStatistics(wdStatisticParagraphs)
    FtrLines = wFRangeFtr.ComputeStatistics(wdStatisticLines)
    End if

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: microsoft word VBA coding that counts word/s in the document

    neo_phyte is looking for a specific word and how many times it appears in the document. The document stastics wil not really be of mush help in this scenerio.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width