Delphi:统计document office中特殊字符串(通配符)的出现次数
我试图计算MS Word文档中出现的特殊字符串的总数。 搜索字符串为:\{F*\} 如何获取搜索字符串的出现次数?有两个选项: 选择1 一种方法是使用代码和循环,直到您无法再找到引用为止。 请参见本网站上的vba代码: 您必须用Delphi翻译下面的代码Delphi:统计document office中特殊字符串(通配符)的出现次数,delphi,Delphi,我试图计算MS Word文档中出现的特殊字符串的总数。 搜索字符串为:\{F*\} 如何获取搜索字符串的出现次数?有两个选项: 选择1 一种方法是使用代码和循环,直到您无法再找到引用为止。 请参见本网站上的vba代码: 您必须用Delphi翻译下面的代码 Sub FindWords() Dim sResponse As String Dim iCount As Integer ' Input different words until the user clicks c
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字符串并搜索该字符串。
如果发生多次,则执行速度可能会更快。
另见:
一个函数,用于查找出现的单词并以数组形式返回它们。 看见 密欧密码:
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;
生成一个无限循环。
如果
它总是返回文档的第一个字符
(Ris [Count-1]: = WordApp.Selection.Text;)
帮助请尝试。如果你陷入困境,问一个问题。问题是什么?你已经发布了代码。它起作用了吗?你有错误吗?我们无法猜测您想要完成什么。如何计算搜索后的出现次数当您谈到winword.exe时,通常使用该产品的官方名称:Microsoft Word。当然,第二种方法的缺点是它会破坏剪贴板。没有其他方法可以获取Word文档的文本内容吗?是的,您可以直接从Word获取文本。但是通过剪贴板的路径会去除Word标记代码,这有助于搜索。请参见此处,如何在不使用MS Word的情况下获取Word文本:WordDoc.Range.get_text应该可以完成这项工作。谢谢Johan-我可以使用第二种方法。因为这对我来说很有趣,只算发生的次数
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;)