以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  关于word多次替换的问题  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=144130)

--  作者:fntfgz
--  发布时间:2019/12/11 16:58:00
--  关于word多次替换的问题
打开一个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次替换都可以成功
如果加上页面设置代码后,只有第一次替换成功
请问老师,问题发生在哪里?

--  作者:有点蓝
--  发布时间:2019/12/11 17:00:00
--  
同一个文件直接在第一次打开的文档Doc里设置就行了,干嘛重新打开一次文档,多余
--  作者:fntfgz
--  发布时间:2019/12/11 17:04:00
--  
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)

With doc.PageSetup
    .TopMargin = 50
    .BottomMargin = 50
    .LeftMargin = 50
    .RightMargin = 50
End With
Doc.save
app.quit

老师,我是在一个里打开着,但还是上面的情况
[此贴子已经被作者于2019/12/11 17:05:05编辑过]

--  作者:fntfgz
--  发布时间:2019/12/11 17:05:00
--  
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:wj - 副本.zip


--  作者:有点蓝
--  发布时间:2019/12/11 17:13:00
--  
我测试没有问题
--  作者:fntfgz
--  发布时间:2019/12/11 17:30:00
--  
老师,我有一个替换是把两个连续的回车,替换为一个,但是我运行后还会有这种情况,老师您运行后,没有这种情况吗?

图片点击可在新窗口打开查看此主题相关图片如下:微信截图_20191211172831.png
图片点击可在新窗口打开查看


--  作者:有点蓝
--  发布时间:2019/12/11 17:41:00
--  
文档有很多是有连续3个连续的回车的呀,替换后肯定还剩下2个呀,再替换一次呗