2013-09-23 108 views
-1

我想要計算MS Word文檔中特殊字符串出現的總次數。 搜索字符串是:(\{F)(*)(\})Delphi:計算文檔中特殊字符串(通配符)的出現次數

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

請不要嘗試。如果您遇到問題,請提出問題。 –

+0

我編輯了帖子 –

+2

,這有什麼問題?你已經發布了代碼。這是工作?你有錯誤嗎?我們無法猜測你想完成什麼。 – RBA

回答

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: 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; 
+0

第二種方法的缺點當然是它會破壞剪貼板。是否有另一種獲取Word文檔的文本內容的方式? –

+0

是的,你可以直接從Word中獲取文本。但是通過剪貼板的路線去掉了Word標記代碼,這有助於搜索。 – Johan

+0

這裏看到這裏如何獲得沒有使用MS Word的Word文本:http://delphi.cjcsoft.net/viewthread.php?tid=44160 – Johan

0

該功能可以找到一個詞的出現和以數組形式返回它們。 看到Word VBA Wildcard Search Match 伊爾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;) 

幫助的第一個字符

相關問題