打开一个word文档,替换3次,然后进行页面设置,代码如下:
Dim wj As String=ProjectPath & "\wj.doc"
Dim app As New MSWord.Application
Dim doc = app.Documents.Open(wj)
app.Selection.Find.ClearFormatting
app.Selection.Find.Replacement.ClearFormatting
With app.Selection.Find
.Text = "^l"
.Replacement.Text = "^p"
.Forward = True
.Wrap = MSWord.WdFindWrap.wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
app.Selection.Find.Execute(Replace:= MSWord.WdReplace.wdReplaceAll)
app.Selection.Find.ClearFormatting
app.Selection.Find.Replacement.ClearFormatting
With app.Selection.Find
.Text = " "
.Replacement.Text = ""
.Forward = True
.Wrap = MSWord.WdFindWrap.wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
app.Selection.Find.Execute(Replace:= MSWord.WdReplace.wdReplaceAll)
app.Selection.Find.ClearFormatting
app.Selection.Find.Replacement.ClearFormatting
With app.Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = MSWord.WdFindWrap.wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
app.Selection.Find.Execute(Replace:= MSWord.WdReplace.wdReplaceAll)
Doc.save
app.quit
Dim app3 As New MSWord.Application
Dim doc3 = app3.Documents.Open(wj)
With doc3.PageSetup
.TopMargin = 50
.BottomMargin = 50
.LeftMargin = 50
.RightMargin = 50
End With
Doc3.save
app3.quit
发生的问题:当只是进行3次替换,删除页面设置代码后,3次替换都可以成功
如果加上页面设置代码后,只有第一次替换成功
请问老师,问题发生在哪里?