我正在尝试计算MS Word文档中特殊字符串的出现次数。
搜索字符串为:(\{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;
如何获取搜索字符串的出现次数?
答案 0 :(得分:2)
有两种选择:
选项1
一种是使用你的代码和循环,直到你不再能够找到事件。
请参阅此站点的vba代码:http://wordribbon.tips.net/T010761_Generating_a_Count_of_Word_Occurrences.html
您必须在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
选项2
另一种选择是将整个文本复制/粘贴到Delphi字符串并搜索。
如果发生的次数很多,则执行速度可能会更快。
另见: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;
答案 1 :(得分:0)
用于查找单词出现并将其返回到数组中的函数。 见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;
生成无限循环。 如果
..
aReplaceWith: = 'any text';
aReplace: = wdReplaceOne;
..
它总是返回文档的第一个字符
(Ris [Count-1]: = WordApp.Selection.Text;)
帮助