|
楼主 |
发表于 2017-11-4 08:55
|
显示全部楼层
本帖最后由 chenwenming 于 2017-11-4 17:44 编辑
再附上我一直崇拜的龚老师的代码(针对附件1):- Sub DelSamePara()
- On Error Resume Next
- Dim ParaXml() As String
- Dim ParaText() As String
- Dim TextPart() As String
- Dim TextNum As Long
- Dim OpenXml() As String
- Dim NeedDel() As Boolean
- Dim XmlPartNum As Long
- Dim ParaNum As Long
- Dim BefBody As String
- Dim AftBody As String
- Dim i As Long
- Dim j As Long
- Dim k As Long
- Dim ParaPartIndex As Long
- Dim SubStart As Long
- OpenXml = Split(ActiveDocument.Range.WordOpenXML, "<pkg:part pkg:name=")
- XmlPartNum = UBound(OpenXml)
- For i = 0 To XmlPartNum
- If Left$(OpenXml(i), 120) = """/word/document.xml"" pkg:contentType=""application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"">" Then
- ParaPartIndex = i
- BefBody = Left$(OpenXml(i), InStr(OpenXml(i), "<w:body>") + 7)
- OpenXml(i) = Right$(OpenXml(i), Len(OpenXml(i)) - Len(BefBody))
- AftBody = Right$(OpenXml(i), Len(OpenXml(i)) - InStrRev(OpenXml(i), "</w:body>") + 1)
- OpenXml(i) = Left$(OpenXml(i), Len(OpenXml(i)) - Len(AftBody))
- ParaXml = Split(OpenXml(i), "</w:p>")
- ParaNum = UBound(ParaXml) - 1
- Exit For
- End If
- Next
- If ParaNum > 0 Then
- ReDim ParaText(ParaNum) As String
- For j = 0 To ParaNum
- TextPart = Split(ParaXml(j), "</w:t>")
- TextNum = UBound(TextPart) - 1
- For i = 0 To TextNum
- TextPart(i) = Right$(TextPart(i), Len(TextPart(i)) - InStrRev(TextPart(i), ">"))
- Next
- TextPart(i) = ""
- ParaText(j) = Replace$(Trim$(Join(TextPart, "")), " ", "")
- Next
- End If
- '下面是你要怎么判断标题开头,标题结束,设置需要删除的规则,你的文档表达不清楚,请根据自己的需要修改。
- ReDim NeedDel(ParaNum) As Boolean
- For i = 0 To ParaNum
- If ParaText(i) Like "#、*" Or ParaText(i) Like "##、*" Or ParaText(i) Like "[一二三四五六七八九○零十百]*" Then
- For j = i - 1 To SubStart Step -1
- For k = j - 1 To SubStart Step -1
- If ParaText(j) = ParaText(k) Then
- NeedDel(j) = True
- Exit For
- End If
- Next
- Next
- SubStart = i
- End If
- Next
- For i = 0 To ParaNum
- If NeedDel(i) = True Then
- ParaXml(i) = Left$(ParaXml(i), InStr(ParaXml(i), "<w:p ") - 1)
- Else
- ParaXml(i) = ParaXml(i) & "</w:p>"
- End If
- Next
- OpenXml(ParaPartIndex) = BefBody & (Join(ParaXml, "") & AftBody)
- ActiveDocument.Range.InsertXML Join(OpenXml, "<pkg:part pkg:name=")
- End Sub
复制代码
|
|