Sub Macro1() Selection.GoTo What:=wdGoToBook, Name:="abc" Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "文本1" .Replacement.Text = "文本2" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchByte = False .CorrectHangulEndings = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute With Selection If .Find.Forward = True Then .Collapse Direction:=wdCollapseStart Else .Collapse Direction:=wdCollapseEnd End If .Find.Execute Replace:=wdReplaceOne If .Find.Forward = True Then .Collapse Direction:=wdCollapseEnd Else .Collapse Direction:=wdCollapseStart End If .Find.Execute End With End Sub
Dim wdapp As Object Dim wddoc As Object Set wdapp = CreateObject("Word.Application") Set wddoc = wdapp.Documents.Open("c:\doc1.doc")
wddoc.Books("abc").Range.text = "文本2"
wddoc.Save wddoc.Close wdapp.Quit Set wdtab = Nothing Set wddoc = Nothing Set wdapp = Nothing
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "文本1"
.Replacement.Text = "文本2"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.CorrectHangulEndings = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
End Sub
Dim wddoc As Object
Set wdapp = CreateObject("Word.Application")
Set wddoc = wdapp.Documents.Open("c:\doc1.doc")
wddoc.Books("abc").Range.text = "文本2"
wddoc.Save
wddoc.Close
wdapp.Quit
Set wdtab = Nothing
Set wddoc = Nothing
Set wdapp = Nothing