Excel VBAでヘッダーテキストを置き換える

Excel VBAでヘッダーテキストを置き換える

MSWord 文書内の特定の単語を置き換えるために、現在のコードを変更しようとしています。 全体的なアイデアは機能していますが、文書の本文に対してのみ機能し、ヘッダーやフッターでは何も起こりません。 どこが間違っているのか教えていただけますか?

よろしくお願いします!

Sub FindAndReplace(ByVal FindWord, ByVal ReplaceWord)

WordApp.ActiveDocument.Range.Find.Execute FindText:=FindWord, Wrap:=1, ReplaceWith:=ReplaceWord, Replace:=2
End Sub

答え1

試す:

Sub FindAndReplace(ByVal FindWord, ByVal ReplaceWord)
Dim oStory as WordApp.Range
For Each oStory in WordApp.ActiveDocument.StoryRanges
    oStory.Find.Execute _
        FindText:=FindWord, ReplaceWith:=ReplaceWord, Wrap:=1, Replace:=2
Next oStory
Set oStory = Nothing
End Sub

関連情報