本帖最后由 jazzson 于 2018-9-14 11:47 编辑
谢谢大家关注,经仔细检查。是因为paragraphs(idx).range.text 中含有 chr(13) 导致插入后文档段.count 不断变化所致。将chr(13) 替换掉再replace 问题解决。谢谢大家
请教一下高手,因为学习需要把附件中的word 文件中的行号和标签行调换。
如2、3、5、6、8、9... 行中如下数据
1.标签:
把他们的顺序改成 标签:1.
但是用下面的代码find 和replace ,会导致word 的paragraph 发生变化,调换失败。请高手帮忙看看该怎样修改代码,才能把Word 的 2、3、5、6、8、9 行,把行号、标签的对应的行改成标签在前面、行号在后面呢?
Sub 测试() Debug.Print "笔记整理开始,笔记文档名称= " & ActiveDocument.Name
If InStr(ActiveDocument.Paragraphs(3).Range.Text, "标签:") = 0 Then Debug.Print "笔记文档 " & ActiveDocument.Name & " 没有导出标签,不需要处理" Exit Sub End If
Debug.Print "整理正式开始,笔记文档名称= " & ActiveDocument.Name
Debug.Print "把所有的标签行都设置成标题 2" 'For idx = 0 To (ActiveDocument.Paragraphs.Count - 1) / 3 - 1 ' For idx = 1 To ActiveDocument.Paragraphs.Count ' idxProcess = idx ' ActiveDocument.Paragraphs(idxProcess).Range.Select ' Selection.ParagraphFormat.Style = "正文" ' Next
For idx = 1 To ActiveDocument.Paragraphs.Count idxProcess = idx If InStr(1, ActiveDocument.Paragraphs(idxProcess).Range.Text, Chr(10), vbBinaryCompare) > 0 Then Debug.Print "行号=" & idx; " 含有换行符,内容=" & ActiveDocument.Paragraphs(idxProcess).Range.Text Else Debug.Print "行号=" & idxProcess; " 没有换行符,内容=" & ActiveDocument.Paragraphs(idxProcess).Range.Text End If Next
' Exit Sub
'文档整理,标签行互换 'For idx = 0 To (ActiveDocument.Paragraphs.Count - 1) / 3 - 1 For idx = (ActiveDocument.Paragraphs.Count - 1) / 3 - 1 To 0 Step -1 'For idx = 1 To 1 sSeqn = ActiveDocument.Paragraphs(idx * 3 + 2).Range.Text sMark = ActiveDocument.Paragraphs(idx * 3 + 3).Range.Text
idxProcess = idx * 3 + 3 Debug.Print ActiveDocument.Name & "准备处理第 " & idxProcess & "行,当前文档段数=" & ActiveDocument.Paragraphs.Count If InStr(1, ActiveDocument.Paragraphs(idxProcess).Range.Text, Chr(10), vbBinaryCompare) > 0 Then Debug.Print "行号=" & idx; " 含有换行符,内容=" & ActiveDocument.Paragraphs(idxProcess).Range.Text Else Debug.Print "行号=" & idxProcess; " 没有换行符,内容=" & ActiveDocument.Paragraphs(idxProcess).Range.Text End If
ActiveDocument.Paragraphs(idxProcess).Range.Select 'Selection.ParagraphFormat.Style = "标题 2" With Selection.Find ' .ClearFormatting .Text = sMark '.Replacement.ClearFormatting .Replacement.Text = sSeqn Debug.Print "执行替换,用 (" & .Replacement.Text & ") 替换 (" & .Text & ")" & " 本意用(" & sSeqn & ") 替换(" & sMark & ")" '.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue .Execute Replace:=wdReplaceAll, Forward:=False, Wrap:=wdFindStop End With
idxProcess = idx * 3 + 2 Debug.Print ActiveDocument.Name & "准备处理第 " & idxProcess & "行,当前文档段数=" & ActiveDocument.Paragraphs.Count If InStr(1, ActiveDocument.Paragraphs(idxProcess).Range.Text, Chr(10), vbBinaryCompare) > 0 Then Debug.Print "行号=" & idx; " 含有换行符,内容=" & ActiveDocument.Paragraphs(idxProcess).Range.Text Else Debug.Print "行号=" & idxProcess; " 没有换行符,内容=" & ActiveDocument.Paragraphs(idxProcess).Range.Text End If
ActiveDocument.Paragraphs(idxProcess).Range.Select With Selection.Find ' .ClearFormatting .Text = sSeqn '.Replacement.ClearFormatting .Replacement.Text = sMark Debug.Print "执行替换,用 (" & .Replacement.Text & ") 替换 (" & .Text & ")" '.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue .Execute Replace:=wdReplaceAll, Forward:=False, Wrap:=wdFindStop End With
' With Selection.Find ' .Text = sSeqn ' .Replacement.Text = sMark ' .Execute Replace:=wdReplaceAll ' End With
' Debug.Print "正在处理第 " & (idx * 2 + 1) & " 行" ' ActiveDocument.Paragraphs(idx * 2 + 1).Range.Select ' With Selection.Find ' .Text = sMark ' .Replacement.Text = sSeqn ' .Execute Replace:=wdReplaceAll ' End With
Next
Debug.Print "文档处理完成"
End Sub
测试数据.rar
(4.16 KB, 下载次数: 1)
|