ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 2126|回复: 12

[求助] 优化整理文档代码

[复制链接]

TA的精华主题

TA的得分主题

发表于 2017-10-18 12:04 | 显示全部楼层 |阅读模式
目的:网校讲义下载合并后,添加标题大纲
功能如下:1、去除文档中各处插入的卖课件人的推销图片
                  2、替换文档中全角空格为半角空格
                  3、设置一、二、三级标题大纳格式
                  4、去除重复的标题大纳
求助1、优化代码;2、解决三级标题前面是表格不能设置的问题
Sub 整理课程标题()
Dim i As Integer '循环执行
Dim myRange As Range
Dim oInlineShape As InlineShape, oShape As Shape
Dim myHeight As Single, myWidth As Single
Dim acon As New Collection
Dim apar As Paragraph
Dim starttime, endtime
On Error Resume Next
starttime = Timer
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'删除指定图片
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    myHeight = 23    '磅,必须以磅为单位,如以厘米等,则会在单位换算过程中出现误差
    myWidth = 415    '磅
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '如 以磅为单位返回需要删除的图形的宽或者高
    '    MsgBox Selection.ShapeRange(1).Width
    '    MsgBox Selection.ShapeRange(1).Height
    '    MsgBox Selection.InlineShapes(1).Width
    '    MsgBox Selection.InlineShapes(1).Height
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Application.ScreenUpdating = False
    '删除指定高度宽度的嵌入式图形
    For Each oInlineShape In ActiveDocument.InlineShapes
        If oInlineShape.Height = myHeight And oInlineShape.Width = myWidth Then oInlineShape.Delete
    Next
    '删除指定宽度高度的浮动式图形
    For Each oShape In ActiveDocument.Shapes
        If oShape.Height = myHeight And oShape.Width = myWidth Then oShape.Delete
    Next

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'替换全角空格为半角空格
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    If Selection.Type = wdSelectionIP Then Selection.WholeStory
    Set myRange = Selection.Range
    myRange.Find.Execute FindText:=" ", replacewith:="  ", Replace:=wdReplaceAll    '替换所有全角空格为半角空格
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'设置标题
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For i = 1 To 100 '假设有100个,循环执行100次 ,设置一级标题
    ' 查找一二三顿号开头的段落
    Selection.Find.ClearFormatting '清除查找框格式
    Selection.Find.Replacement.ClearFormatting  '清除替换框格式
    With Selection.Find
        .Text = "(第[一二三四五六七八九十]{1,}章 )(*^13)"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute ' 设置字体字号 Macro
    Selection.Font.Name = "仿宋" '字体
    Selection.Font.Bold = True '加粗
    Selection.Font.Size = 16 '字号,14=四号;16=三号……
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter  '居中对齐
    Selection.ParagraphFormat.OutlineLevel = wdOutlineLevel1   '设为标题1
Next i
For i = 1 To 100 '假设有100个,循环执行100次 标题二设置
    ' 查找一二三顿号开头的段落
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "(第[一二三四五六七八九十]{1,}节 )(*^13)"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute ' 设置字体字号 Macro
    Selection.Font.Name = "等线(中文正文)" '字体
    Selection.Font.Bold = True '加粗
    Selection.Font.Size = 12 '字号,14=四号;16=三号……
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter  '居中对齐
    Selection.ParagraphFormat.OutlineLevel = wdOutlineLevel2   '设为标题1
Next i
For i = 1 To 500 '假设有100个,循环执行100次 标题三设置
    ' 查找一二三顿号开头的段落
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "(^13[一二三四五六七八九十]{1,}、)(*^13)" '未能解决标题前是表格的,就设置不了,求解决?
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute ' 设置字体字号 Macro
    Selection.MoveStart Unit:=wdCharacter, Count:=1
    Selection.Paragraphs(1).Range.Select
    Selection.Font.Name = "等线(中文正文)" '字体
    Selection.Font.Bold = True '加粗
    Selection.Font.Size = 10.5 '字号,14=四号;16=三号……
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft  '居中对齐
    Selection.ParagraphFormat.OutlineLevel = wdOutlineLevel3  '设为标题1
Next i
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'删除重复标题
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For Each apar In ActiveDocument.Paragraphs
   acon.Add apar.Range, VBA.CStr(apar.Range.Text)
   If Err.Number = 457 Then
        apar.Range.Delete   '直接删除重复段落
        'apar.Range.Font.Color = wdColorRed  '标记重复段落为红色
        Err.Clear
        End If
Next
Application.ScreenUpdating = True
endtime = Timer
MsgBox "用时:  " & endtime - startime
End Sub



TA的精华主题

TA的得分主题

 楼主| 发表于 2017-10-23 08:58 | 显示全部楼层
各大版主、能手、高阶,能指点一二吗?

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-10-27 09:33 | 显示全部楼层
自己优化了一个

Sub 整理课程标题()
Dim i As Integer '循环执行
Dim myRange As Range
Dim oInlineShape As InlineShape, oShape As Shape
Dim myHeight As Single, myWidth As Single
Dim acon As New Collection
Dim apar As Paragraph
Dim starttime, endtime
Dim MyStr As String, YouStr As String
On Error Resume Next
starttime = Timer
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'删除指定图片
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    myHeight = 23    '磅,必须以磅为单位,如以厘米等,则会在单位换算过程中出现误差
    myWidth = 415    '磅
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '如 以磅为单位返回需要删除的图形的宽或者高
    '    MsgBox Selection.ShapeRange(1).Width
    '    MsgBox Selection.ShapeRange(1).Height
    '    MsgBox Selection.InlineShapes(1).Width
    '    MsgBox Selection.InlineShapes(1).Height
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Application.ScreenUpdating = False
    '删除指定高度宽度的嵌入式图形
    For Each oInlineShape In ActiveDocument.InlineShapes
        If oInlineShape.Height = myHeight And oInlineShape.Width = myWidth Then oInlineShape.Delete
    Next
    '删除指定宽度高度的浮动式图形
    For Each oShape In ActiveDocument.Shapes
        If oShape.Height = myHeight And oShape.Width = myWidth Then oShape.Delete
    Next

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'替换全角空格为半角空格
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    If Selection.Type = wdSelectionIP Then Selection.WholeStory
    Set myRange = Selection.Range
    myRange.Find.Execute FindText:=" ", replacewith:="  ", Replace:=wdReplaceAll    '替换所有全角空格为半角空格
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'设置标题
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
MyStr = "第"
YouStr = "一二三四五六七八九十"
With ActiveDocument
    For Each apar In .Paragraphs
      If apar.Range.Information(wdWithInTable) = False Then
           If apar.Range.Find.Execute(FindText:="第[一二三四五六七八九十]{1,}章 ", Forward:=True, MatchWildcards:=True) = True And InStr(MyStr, ActiveDocument.Range(apar.Range.Start, apar.Range.Start + 1).Text) > 0 Then
             '.Style = wdStyleHeading3
             With apar.Range
             .Font.Name = "仿宋" '字体
             .Font.Bold = True '加粗
             .Font.Size = 16 '字号,14=四号;16=三号……
             .ParagraphFormat.Alignment = wdAlignParagraphCenter  '居中对齐
             .ParagraphFormat.OutlineLevel = wdOutlineLevel1   '设为标题1
             End With
             ElseIf apar.Range.Find.Execute(FindText:="第[一二三四五六七八九十]{1,}节 ", Forward:=True, MatchWildcards:=True) = True And InStr(MyStr, ActiveDocument.Range(apar.Range.Start, apar.Range.Start + 1).Text) > 0 Then
             With apar.Range
             .Font.Name = "等线(中文正文)" '字体
             .Font.Bold = True '加粗
             .Font.Size = 12 '字号,14=四号;16=三号……
             .ParagraphFormat.Alignment = wdAlignParagraphCenter  '居中对齐
             .ParagraphFormat.OutlineLevel = wdOutlineLevel2   '设为标题1
             End With
             ElseIf apar.Range.Find.Execute(FindText:="[一二三四五六七八九十]{1,}、", Forward:=True, MatchWildcards:=True) = True And InStr(YouStr, ActiveDocument.Range(apar.Range.Start, apar.Range.Start + 1).Text) > 0 Then
             With apar.Range
             .Font.Name = "等线(中文正文)" '字体
             .Font.Bold = True '加粗
             .Font.Size = 10.5 '字号,14=四号;16=三号……
             .ParagraphFormat.Alignment = wdAlignParagraphLeft  '居中对齐
             .ParagraphFormat.OutlineLevel = wdOutlineLevel3  '设为标题1
              End With
             End If
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'删除重复标题
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
   acon.Add apar.Range, VBA.CStr(apar.Range.Text)
   If Err.Number = 457 Then
        apar.Range.Delete   '直接删除重复段落
        'apar.Range.Font.Color = wdColorRed  '标记重复段落为红色
        Err.Clear
        End If

   End If
    Next
End With


Application.ScreenUpdating = True
endtime = Timer
MsgBox "用时:  " & endtime - startime
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-10-27 12:02 | 显示全部楼层
还是有BUG,主要原因是重复段落的删除不对,一个章节中的重复段落才删除,非一个章节重复不删除,这代码将所有章节重复段落删除了,我的去,我得想办法将章节标题合并后才删除才不错删除。怎么弄啊,找资料去。。。。。。

TA的精华主题

TA的得分主题

发表于 2017-11-13 11:43 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
关于优化,我觉得使用For循环的效率比较低,因为你不知道到底有多少个,for 100可能只用到十几个,可以改成do while true

关于三级标题那个,我也有类似的问题,主要是通配符没法定位段首,而段落上有表格的话,又没法用^13,解决的思路是使用正则,但WD正则我也不会。。。楼主可以如果会这方面,可以试一下,有解决的方法也告诉我一下哈~

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-8-1 13:30 | 显示全部楼层
iamadsl 发表于 2017-11-13 11:43
关于优化,我觉得使用For循环的效率比较低,因为你不知道到底有多少个,for 100可能只用到十几个,可以改成 ...

找到个正则大师,没学会;大师脾气较大;找江科大的垄老师,给写了个网页语言的,也没搞懂。

TA的精华主题

TA的得分主题

发表于 2019-8-1 20:26 | 显示全部楼层
本帖最后由 413191246se 于 2019-8-2 08:25 编辑

* 楼主,你好!——我有 Word 自动排版宏,建议试用(重复的大纲标题,可以全部设置为标题样式后,用“文档结构图”来观察删除)。
* 另外,建议 楼主 能否提供一个附件,说明要求,就像大家不愿意看我的代码一样,我也不愿意看你的代码,嫌多,不如重新编码。


* 排版分两种:一是纯文本(无表格),直接全选再设置正文、标题即可;二是有表格文档,过去我是不管有无表格一律当成无表格来排版,清除格式;但现在,一般情况是别人已经编辑好的表格,所以不宜再清除格式了,费那个时间也没有必要,所以新理念就是:有表格文档排版时,最快的方法就是,首先让表格无环绕,然后,分别取表格间的区域 Range 进行区域(块状)排版(无表格文本实际上就是最大的区域/块状),这样速度极快(但超大文本排版还有问题,我觉得是 Word 不断分页的原因,会含有很多“节”)。像“第X章/第X节”这样的标题,我已经有宏,一键即可自动设置格式。

* 比如:选定从文首到第一个表格间的区域,即 ActiveDocument.Range(Start:=0,End:=.Tables(1).Range.Start).Select


* 既然是区域排版,区域上面有表格,只须将区域前面加一个回车即可,即 If .Start <> 0 Then .InsertParagraphBefore

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-8-9 09:15 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
413191246se 发表于 2019-8-1 20:26
* 楼主,你好!——我有 Word 自动排版宏,建议试用(重复的大纲标题,可以全部设置为标题样式后,用“文档 ...

可否提供宏代码我试试

TA的精华主题

TA的得分主题

发表于 2019-8-9 09:56 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-8-9 11:58 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-24 22:52 , Processed in 0.035820 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表