Results 1 to 8 of 8

Thread: ** Resolved ** Word 2000 VBA Word Count Question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Edgerton, WI
    Posts
    381

    Resolved ** Resolved ** Word 2000 VBA Word Count Question

    When I prepare my weekly sermon, I usually manuscipt my notes and use them as a general guide for what I intend to speak about. Helps me keep focused and on time. Usually when I step into the pulpit, after announcing the message title, I usually announce the key word or phrase to watch for and count during the course of the message. This is primarily for the children and youth but I've found that many of the adults come up after church and say something like, "Pastor, I think you used the word 'justified' about 19 times in your message."

    What I'd like to be able to do is write some VBA code that when executed would popup a message box that would prompt me to enter a key word of phrase to be counted and upon entering it and pressing ok, it would proceed to count the number of instances the key word or phrase is used in the document.

    I would like to be able, if possible to launch this VBA code from a button on a toolbar if possible. I am assuming this code would need to be in the Normal.dot templete so that the VBA code code be used with any document I create.

    I've done a little VB6 prgramming of database applications for my church and office but no real VBA programming in Word.

    Any help with examples would be greaty apprciated.

    Thanks,
    Pastor Mike
    Last edited by Rev. Michael L. Burns; Apr 15th, 2005 at 11:33 AM.

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

    Re: Word 2000 VBA Word Count Question

    This will run a search for a word or phrase that you get prompted for and display a message box with the total.
    You can easily modify it for case sensitivity, like words, etc.

    VB Code:
    1. Private Sub CountMe()
    2.     Dim sWord As String
    3.     Dim iCount As Integer
    4.     sWord = InputBox("Enter the 'key word' or 'phrase' to count.", "Key word or Phrase", vbNullString)
    5.     If Len(sWord) = 0 Then Exit Sub
    6.     iCount = 0
    7.     ActiveDocument.Select
    8.     With Selection.Find
    9.         .ClearFormatting
    10.         .Style = wdStyleBodyText
    11.         .MatchCase = False
    12.         .MatchWholeWord = True
    13.         .MatchWildcards = False
    14.         .MatchSoundsLike = False
    15.         .MatchAllWordForms = False
    16.         .Wrap = wdFindContinue
    17.         Do While .Execute(FindText:=sWord, Forward:=True, Wrap:=False, Format:=False) = True
    18.             If .Found = True Then
    19.                 iCount = iCount + 1
    20.             End If
    21.         Loop
    22.         MsgBox iCount & " instances of '" & sWord & "'!", vbOKOnly + vbInformation
    23.     End With
    24. End Sub
    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Edgerton, WI
    Posts
    381

    Re: Word 2000 VBA Word Count Question

    Thanks Rob. It wrks great. Another stupid question, how would I run this from button on the Word toolbar?

    Mchael
    Last edited by Rev. Michael L. Burns; Apr 14th, 2005 at 04:31 PM.

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

    Re: Word 2000 VBA Word Count Question

    Change CountMe so its Public instead of Private. Paste it into the Normal VBA IDE window.
    Then create a new toolbar: View > ToolBars > Customize....
    In the Toolbars tab click "New..." and name your toolbar. Select to make it available to Normal.dot. Now your
    balnk toolbar will be showing.

    To assign the CountMe macro to a new button...
    Commands tab > select Macros > select "Normal.ThisDocument.CountMe"
    Drag the selection to your new toolbar and drop it there.
    You can now customize it.

    Click the "Modify Selection" button which now becomes enabled. In the Name menu item click in there and rename
    the "Normal.ThisDocument.CountMe" to "CountMe". You toolbar button will change caption to "CountMe".

    You can assign an image to the button also...
    Click the "Modify Selection" button again and select the "Change Button Image" menu. Then select an image from the list.
    Now you can click ok and close it. You can then drag your toolbar to the main toolbar docking area and
    arrange it to suit your needs.

    You can also assign the macro to a keyboard shortcut

    Enjoy
    Attached Images Attached Images  
    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
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Edgerton, WI
    Posts
    381

    Re: Word 2000 VBA Word Count Question

    Thanks Rob. I hate to be a pest but apparently I am doing something wrong or missing something. The macr works fine. However, I am having a problem when attempting to place it on a toolbar.

    You said, "In the Toolbars tab click "New..." and name your toolbar. Select to make it available to Normal.dot. Now your blank toolbar will be showing." When I do this, the toolbar immediately appears in the middle of the page and cannnot be moved.

    As a work around, I placed a button on an existing toolbar (the standard wordcount bar) and attempted to change the name as you suggested (Click the "Modify Selection" button which now becomes enabled. In the Name menu item click in there and rename the "Normal.ThisDocument.CountMe" to "CountMe". You toolbar button will change caption to "CountMe".)

    However, the "Modify Selection" is not visable at all when Macros is selected.

    Other than that it works great.

    Michael

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

    Re: Word 2000 VBA Word Count Question

    When adding the macro to your custom toolbar make sure the toolbar is selected in the toolbar tab before
    proceeding. Then you can drag a macro to it to create a new button. The Modify Selection will become visible/enabled.
    The toolbar should always become visible right after creation and be moveable.

    Did you paste the macro code into the Normal.dot ThisDocument class? and change it from Private to Public?

    When you select the Macros item, does the CountMe macro list in the little window on the right of it? If it doesnt
    then its either still Private or its not in the Normal.Dot template. This could be why the Modify Selection button is not visible.

    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
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Edgerton, WI
    Posts
    381

    Re: Word 2000 VBA Word Count Question

    Thanks Rob. It seems to be working now. The toolbar became visable immediately but was not movable until I place a button on it. After that everything worked ok.

    Michael

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

    Re: ** Resolved ** Word 2000 VBA Word Count Question

    Cool!

    Just wanted to mention that the code may need to be tweeked a bit if you need to count other areas like
    Headers/Footers/Headings, etc.

    The line - ".Style = wdStyleBodyText" sets the Find to look only at the body text. There are other setting like I mentioned
    but if your just using a basic document layout then you should be ok.

    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