-1

I'm trying to count the total number of occurrences of a special string in a MS Word document. The search string is:(\{F)(*)(\}).

function CountOcc(SString:string): Integer;
var
   aFindText, aMatchCase,aWrap,AMatchWholeWord,aReplaceWith,aReplace: OleVariant;
   Result1: boolean
begin
   Result := False;
   aFindText := SString;
   aMatchCase := false;
   aMatchWholeWord := true;
   aWrap := wdFindContinue;
   aReplace:=wdReplaceNone;
   aMatchWildCards:=true;
   aReplaceWith:=SString;
   try
     Result1:=WordContainer.OleObject.ActiveWindow.Selection.Range.Find.Execute(
                aFindText
              , aMatchCase
              , aMatchWholeWord
              , aMatchWildCards
              , EmptyParam, EmptyParam, EmptyParam, aWrap, EmptyParam
              , aReplaceWith, aReplace
              , EmptyParam, EmptyParam,EmptyParam, EmptyParam);
   finally
     if Result1 then ........
   end;
end;

How do I get the number of occurrences of the search string?

Johan
  • 74,508
  • 24
  • 191
  • 319

2 Answers2

2

There are two options:

Option 1
One is to use your code and loop until you can no longer find occurrences. See the vba code from the this site: http://wordribbon.tips.net/T010761_Generating_a_Count_of_Word_Occurrences.html

You'll have to translate the below code in Delphi.

Sub FindWords()
    Dim sResponse As String
    Dim iCount As Integer

    ' Input different words until the user clicks cancel
    Do
        ' Identify the word to count
        sResponse = InputBox( _
          Prompt:="What word do you want to count?", _
          Title:="Count Words", Default:="")

        If sResponse > "" Then
            ' Set the counter to zero for each loop
            iCount = 0
            Application.ScreenUpdating = False
            With Selection
                .HomeKey Unit:=wdStory
                With .Find
                    .ClearFormatting
                    .Text = sResponse
                    ' Loop until Word can no longer
                    ' find the search string and
                    ' count each instance
                    Do While .Execute
                        iCount = iCount + 1
                        Selection.MoveRight
                    Loop
                End With
                ' show the number of occurences
                MsgBox sResponse & " appears " & iCount & " times"
            End With
            Application.ScreenUpdating = True
        End If
    Loop While sResponse <> ""
End Sub

Option 2
The other option is to copy/paste the entire text to a Delphi string and search that.
If there are many occurrences, this may execute faster. See also: Delphi: count number of times a string occurs in another string

....
uses Clipbrd;
....

function Occurrences(const Substring, Text: string): integer; //thx Andries
var
  offset: integer;
begin
  result := 0;
  offset := PosEx(Substring, Text, 1);
  while offset <> 0 do
  begin
    inc(result);
    offset := PosEx(Substring, Text, offset + length(Substring));
  end;
end;

function GetCount(what: string): integer;
var
  CopyOfText: string;
  i: integer;
begin
  WordContainer.OleObject.ActiveWindow.SelectAll;
  WordContainer.OleObject.ActiveWindow.Copy;
  CopyOfText:= Clipboard.AsText;
  Result:= Occurrences(what, CopyOfText);
end;
Community
  • 1
  • 1
Johan
  • 74,508
  • 24
  • 191
  • 319
  • The downside of the second approach, of course, is that it clobbers the clipboard. Isn't there another way of getting the textual contents of a Word document? – Rob Kennedy Sep 23 '13 at 19:15
  • Yes, you can get the text directly from Word. But the route via the clipboard strips out the Word markup codes, which helps with searching. – Johan Sep 23 '13 at 19:20
  • See here here how to get the Word text without even using MS Word: http://delphi.cjcsoft.net/viewthread.php?tid=44160 – Johan Sep 23 '13 at 19:22
  • `WordDoc.Range.Get_Text` should do the job. – Johan Sep 23 '13 at 19:25
  • thanks Johan - I can work with the second approach. Because its interesting to me only count the occurrences – Domenico Formoso Sep 23 '13 at 22:27
0

A function to find the occurrences of a word and returns them in an array. see Word VBA Wildcard Search Match Il mio codice:

function TForm1.Esiste(SString:string): TArr;
var
   aFindText, aMatchWildCards, aMatchCase,aWrap,aMatchAllWordForms,
   AMatchWholeWord,aReplaceWith,aReplace,aForward: OleVariant;
   Count:integer;
   ris : TArr;
begin
   Count:=0;
   aFindText := SString;
   aForward:=True;
   aWrap := wdFindContinue;
   aMatchWildCards:=true;
   aMatchCase := false;
   aMatchWholeWord := true;
   aMatchAllWordForms:=false;
   aReplaceWith := '';
   aReplace:=wdReplaceNone;
   while WordApp.Selection.Range.Find.Execute(
                aFindText
              , aMatchCase
              , aMatchWholeWord
              , aMatchWildCards
              , EmptyParam, aMatchAllWordForms, aForward, aWrap, EmptyParam
              , aReplaceWith, aReplace
              , EmptyParam, EmptyParam,EmptyParam, EmptyParam) do begin
               Count:=count+1;
               SetLength(ris,Count);
               Ris[Count-1]:=WordApp.Selection.Text;
   end;
   Result:=Ris;
end;

generates an infinite loop while. If

..
aReplaceWith: = 'any text';
aReplace: = wdReplaceOne;
..

It always returns the first character of the document

(Ris [Count-1]: = WordApp.Selection.Text;)

Help

Community
  • 1
  • 1