2012-02-08 10 views
1

文書にテキストが含まれているかどうかを確認しようとしていますが、このテキストはヘッダーにのみ存在します。 (私は、ヘッダー/フッターが含まれていません.Contentを想定)以下でも動作するようには思えない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 

:これは、常にテキストが存在するにもかかわらず、falseを返す私が使用していたコードである

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

あなたはおそらく間違ったセクション/見出しを検索しようとしています。あなたはこのコードを試みることができる:

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

ありがとうございました。私は別のサイトでその答えを見つけました、そして、それは当初考えるよりもはるかに複雑です。投稿をチェックしてください! –