VB Scripting in MS Word

Associate
Joined
2 Mar 2004
Posts
706
Location
Shropshire
Hi all,

I'm after a little help. I'd like to get a script together that allows the user to quickly search through large Word documents for 1 or maybe more key words and will return possibly a form back listing how many of said keywords were found.

For example I open up a technical document, run the script which prompts me with a dialog box/form where I input 1 or more keywords and hit go/find. It does it's stuff and then displays a different form detailing the words searched for and the amount of times they were found.

I could just use the find function, but that is rather limiting.

Any help, or good sources to look at would be really appreciated.

Cheers.
 
Hi there,

Ive done some vba stuff with word before the easest method is to record a macro what your trying to do and cutting out the bits you require i saddly only have word at work soooo i can help you in a weeks time when im back from my holiday :)
 
Here you are, pop this in a macro: -

Code:
Sub WordCount()
    Dim sResponse As String
    Dim iCount As Integer
    Do
        sResponse = InputBox(Prompt:="What word do you want to count?", Title:="Count Words", Default:="")
        If sResponse > "" Then
            iCount = 0
            Application.ScreenUpdating = False
            With Selection
                .HomeKey Unit:=wdStory
                With .Find
                    .ClearFormatting
                    .Text = sResponse
                    Do While .Execute
                        iCount = iCount + 1
                        Selection.MoveRight
                    Loop
                End With
                MsgBox sResponse & " appears " & iCount & " times"
            End With
            Application.ScreenUpdating = True
        End If
    Loop While sResponse <> ""
End Sub

Just tested it works ok any problems let me know.:)
 
Back
Top Bottom