2014-10-27 69 views
2

該程序使用RegExp方法在MS Word文檔中查找某些單詞。找到每個匹配項後,程序應找到每個匹配的頁面並創建一個可輸出的字符串,以顯示發現該關鍵字匹配的所有頁面。它現在寫的方式,出於某種原因,它爲每個頁面輸出一個「1」,而不管它在哪個頁面上找到。例如,如果在第1,5和22頁上找到單詞「鼠標」,它將輸出1,1,1 ,.爲什麼這個VB程序總是返回1作爲頁碼?

For Each Match In RegExp.Execute(oWord.ActiveDocument.Range.Text) 
      myKeyWords(numKeywords) = Match.Value 
    PageNumbers(numKeywords) = "" 

    With myWordDoc.ActiveDocument.Range.Find 
     .ClearFormatting() 
     .Text = Match.value 
     .Wrap = False 
     .Forward = True 
     Do While .Execute = True 
      If PageNumbers(numKeywords) = "" Then 
       PageNumbers(numKeywords) = oWord.Selection.Information(Word.WdInformation.wdActiveEndAdjustedPageNumber) 
      Else 
       PageNumbers(numKeywords) = PageNumbers(numKeywords) & ", " & oWord.Selection.Information(Word.WdInformation.wdActiveEndAdjustedPageNumber) 
      End If 
     Loop 
    End With 
+0

我想你的問題是PageNumbers(numKeywords)總是「」。您需要評估此方法和/或numKeywords以確保它們都產生預期結果。而不是SourceString = SourceString +「,..」你可以將表達式寫成SourceString + =「,..」它更整潔,並且可以節省輸入。 – 2014-10-28 06:42:14

+1

PageNumbers(numKeywords)並不總是「」它在If Else語句期間實際上正在改變,但每次For Each Match語句開始時都會重置。感謝您的其他信息。 – Simmons 2014-10-28 18:32:15

回答

1

numKeywords不增加。我寫出來,而不

For Each Match In RegExp.Execute(oWord.ActiveDocument.Range.Text) 

這,要知道,如果你把這個功能,你需要改變。文本= myKeywords(x)和去除的正則表達式的方法外.range找到之前。

PageNumbers(numKeywords) = "" 
For x = LBound(myKeywords) To UBound(myKeywords) 
    PageNumbers(x) = "" 
    With myWordDoc.ActiveDocument.Find 
     .ClearFormatting() 
     .Text = myKeywords(x) 
     .Wrap = False 
     .Forward = True 
     Do While .Execute = True 
      If PageNumbers(numKeywords) = "" Then 
       PageNumbers(numKeywords) = oWord.Selection.Information(Word.WdInformation.wdActiveEndAdjustedPageNumber) 
      Else 
       PageNumbers(numKeywords) = PageNumbers(numKeywords) & ", " & oWord.Selection.Information(Word.WdInformation.wdActiveEndAdjustedPageNumber) 
      End If 
     Loop 
    End With 
相關問題