2012-02-08 215 views
1

我想確認一個文檔是否包含一些文本,唯一的問題是這個文本是在標題中。這是我使用的代碼,不斷地返回false即使文本存在:正在Word文檔的標題部分中搜索文本

Set CurrentDoc = Documents.Open("a.doc") 

With CurrentDoc.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Find 
    .Text = "This is the text to find" 
    .Forward = True 
    .Execute 
    If (.Found = True) Then Debug.Print "Match" 
End With 

下也似乎不工作(我假設.Content不包括頁眉/頁腳):

With CurrentDoc.Content.Find 
    .Text = "This is the text to find" 
    .Forward = True 
    .Execute 
    If (.Found = True) Then Debug.Print "Match" 
End With 

任何幫助將不勝感激。

回答

1

我發現本網站上的答案,這比最初想象的複雜得多:http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm

下面的代碼是從網站上面,除了搜索整個文件中包含文本替換功能:

Public Sub FindReplaceAnywhere() 
    Dim rngStory As Word.Range 
    Dim pFindTxt As String 
    Dim pReplaceTxt As String 
    Dim lngJunk As Long 
    Dim oShp As Shape 

    pFindTxt = InputBox("Enter the text that you want to find.", "FIND") 

    If pFindTxt = "" Then 
     MsgBox "Cancelled by User" 
     Exit Sub 
    End If 

    TryAgain: 
     pReplaceTxt = InputBox("Enter the replacement." , "REPLACE") 

     If pReplaceTxt = "" Then 
      If MsgBox("Do you just want to delete the found text?", vbYesNoCancel) = vbNo Then 
       GoTo TryAgain 
      ElseIf vbCancel Then 
       MsgBox "Cancelled by User." 
      Exit Sub 
     End If 
    End If 

    'Fix the skipped blank Header/Footer problem 
    lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType 

    'Iterate through all story types in the current document 
    For Each rngStory In ActiveDocument.StoryRanges 

     'Iterate through all linked stories 
     Do 
      SearchAndReplaceInStory rngStory, pFindTxt, pReplaceTxt 
      On Error Resume Next 
      Select Case rngStory.StoryType 
       Case WdStoryType.wdEvenPagesHeaderStory, _ 
        WdStoryType.wdPrimaryHeaderStory, _ 
        WdStoryType.wdEvenPagesFooterStory, _ 
        WdStoryType.wdPrimaryFooterStory, _ 
        WdStoryType.wdFirstPageHeaderStory, _ 
        WdStoryType.wdFirstPageFooterStory 
        If rngStory.ShapeRange.Count > 0 Then 
         For Each oShp In rngStory.ShapeRange 
          If oShp.TextFrame.HasText Then 
           SearchAndReplaceInStory oShp.TextFrame.TextRange, pFindTxt, pReplaceTxt 
          End If 
         Next 
        End If 
       Case Else 
        'Do Nothing 
       End Select 
       On Error GoTo 0 

       'Get next linked story (if any) 
       Set rngStory = rngStory.NextStoryRange 
      Loop Until rngStory Is Nothing 
     Next 
End Sub 

Public Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, ByVal strSearch As String , ByVal strReplace As String) 
    With rngStory.Find 
     .ClearFormatting 
     .Replacement.ClearFormatting 
     .Text = strSearch 
     .Replacement.Text = strReplace 
     .Wrap = wdFindContinue 
     .Execute Replace:=wdReplaceAll 
    End With 
End Sub 
2

您可能試圖在錯誤的section/headertype中搜索。你可以試試這個代碼:

Dim rng As Range 
Dim intSecCount As Integer 
Dim intHFType As Integer 
intSecCount = ActiveDocument.Sections.Count 
For intSection = 1 To intSecCount 
    With ActiveDocument.Sections(intSection) 
     For intHFType = 1 To 3 
      Set rng = ActiveDocument.Sections(intSection).Headers(intHFType).Range 
      rng.Find.Execute findtext:="This is the text to find", Forward:=True 
      If rng.Find.Found = True Then 
       Debug.Print "Match" 
      End If 
     Next intHFType 
    End With 
Next intSection 
+0

謝謝,這很有幫助。我在另一個網站上找到了答案,這比最初想象的複雜得多。看看帖子! – 2012-02-09 00:03:56