ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] word排版

[复制链接]

TA的精华主题

TA的得分主题

发表于 2021-5-10 14:35 | 显示全部楼层 |阅读模式
word文档排版,按照格式要求,设置字体,便牵头,页码,段落,等等问题,一键快速排版
360桌面截图20210510143042.jpg

格式要求.zip

22.3 KB, 下载次数: 24

TA的精华主题

TA的得分主题

发表于 2021-5-10 23:39 | 显示全部楼层
* Word 中按 Alt+F11 再按 Ctrl+M 导入 MemoPad.bas 宏模块后,退出 Word,重新打开,按 F8 即可。

TA的精华主题

TA的得分主题

发表于 2021-5-10 23:39 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 413191246se 于 2021-6-1 16:58 编辑

* 略。。。。

TA的精华主题

TA的得分主题

发表于 2021-5-11 08:56 | 显示全部楼层
感谢分享,论坛因你而精彩!



    We meet by chance like patches of drifting duckweed,  
    May everything be fine with you everyday !
      (你我萍水相逢,愿君一切安好!)


    TA的精华主题

    TA的得分主题

     楼主| 发表于 2021-5-11 10:55 | 显示全部楼层

    可以加你微信吗?有些问题需要请教你?15975280786我的微信号。

    TA的精华主题

    TA的得分主题

    发表于 2021-5-11 14:30 | 显示全部楼层
    楼主朋友,我还没有手机,也没有微信,有问题请发帖吧!——说实话,你对公文格式的看法,和我完全不一样(不知你的公文标准是谁规定的?)。我在单位和家里可以随时上论坛查看。

    TA的精华主题

    TA的得分主题

     楼主| 发表于 2021-5-14 08:48 | 显示全部楼层
    413191246se 发表于 2021-5-11 14:30
    楼主朋友,我还没有手机,也没有微信,有问题请发帖吧!——说实话,你对公文格式的看法,和我完全不一样( ...

    地方不同,所以要求的格式也不一样。我试了好多遍了,还是出现不懂的情况。

    TA的精华主题

    TA的得分主题

     楼主| 发表于 2021-5-14 08:49 | 显示全部楼层
    458863601 发表于 2021-5-14 08:48
    地方不同,所以要求的格式也不一样。我试了好多遍了,还是出现不懂的情况。

    Sub 文档初始化() '公文格式初始化
    Selection.WholeStory '选择word 所有文档
    Selection.ClearFormatting '文档格式清除
    Selection.Range.HighlightColorIndex = wdNoHighlight '突出显示文本取消

    With Selection.Paragraphs '段落设置
    .Alignment = wdAlignParagraphLeft '左对齐
    .LineSpacingRule = wdLineSpace1pt5 '行距固定1.5
    .IndentFirstLineCharWidth 3 '首行缩进2个字符
    End With

    With Selection.Font  '字体设置
            .Name = "仿宋_GB2312" '字体名称
            .Size = 16 '三号字体
            .ColorIndex = wdBlack '黑色
            
    End With
    End Sub


    Sub 标题正文设置()

    With Selection.PageSetup              '页面设置
    .TopMargin = CentimetersToPoints(3.7)      '顶端边距
    .BottomMargin = CentimetersToPoints(3.5)   '底端边距
    .LeftMargin = CentimetersToPoints(2.8)     '左边距
    .RightMargin = CentimetersToPoints(2.6)    '右边距
    '.PageWidth = CentimetersToPoints(18.2)   '页面宽度
    '.PageHeight = CentimetersToPoints(25.7)  '页面高度
    End With

    '字体设置
    Dim title_reg, f_reg, s_reg, th_reg, fr_reg, k, mh, strA$
    Set myRange = ActiveDocument.Content
    ' 正则表达式 获取文档内容
    strA = myRange.Text
    Set title_reg = CreateObject("vbscript.regexp")
    Set f_reg = CreateObject("vbscript.regexp")
    Set s_reg = CreateObject("vbscript.regexp")
    Set th_reg = CreateObject("vbscript.regexp")

      Selection.HomeKey unit:=wdStory '光标回到文章开头

    t = 0
    title_reg.Pattern = "\r\r"
    '[^\r]除了换行符之外的所有字符

    title_reg.Global = True
    Set Title = title_reg.Execute(strA)

    With Selection.Find
    .ClearFormatting
    .Text = Title.Item(0)
    .Execute Forward:=True
    Selection.HomeKey unit:=wdStory, Extend:=wdExtend
    End With
    '选择有两个换行符的至开头的所有段落

    With Selection.Font
    .Name = "方正小标宋简体"
    .Size = 22
    .ColorIndex = wdBlack
    End With

    With Selection.Paragraphs '设置行距
    .FirstLineIndent = CentimetersToPoints(0) '取消首行缩进
    .Alignment = wdAlignParagraphCenter '段落居中
    .LineSpacingRule = wdLineSpaceExactly '行距固定
    .LineSpacing = Word.Application.LinesToPoints(2.3) '行距为2.3倍行距 一行距=12
    End With

    ' 以下是设置一级标题
    t1 = 0 '初始化t1,作为一级标题是否是一是二是三是的标记,如果是,则为1,不是则为0
    Selection.HomeKey unit:=wdStory
    f_reg.Pattern = "(一、|二、|三、|四、|五、|六、)[^\r]*\r"
    f_reg.Global = True
    Set f_titles = f_reg.Execute(strA)
    If f_titles.Count = 0 Then '如果一级标题是一是二是三是,则匹配
    f_reg.Pattern = "(一是|二是|三是|四是|五是|六是)([^。])*。"
    Set f_titles = f_reg.Execute(strA)
    t1 = 1
    End If
      
    For Each f_title In f_titles
      With Selection.Find
    .ClearFormatting
    .Text = f_title.Value
    Debug.Print "一级标题遍历项目:"; f_title.Value
    .Execute Forward:=True
    End With

    With Selection.Font
            .Name = "黑体"
            .Size = "16"
            .ColorIndex = wdBlack
            End With
    Selection.HomeKey unit:=wdStory
    Next

    ' 以下是设置二级标题
    If t1 = 0 Then 'p判断一级标题是否是一是二是三是的标记,如果是0,则不是一是二是三是,则执行,不是则不执行
    t2 = 0
    Selection.HomeKey unit:=wdStory
    s_reg.Global = True
    s_reg.Pattern = "((一)|(二)|(三)|(四)|(五)|(六))([^。\r:])*[。|\r:]" '排除句号和段落符号查找所有,找到句号或段落符号后停止
    Set s_titles = s_reg.Execute(strA)

    If s_titles.Count = 0 Then '如果二级标题是一是二是三是,则匹配
    s_reg.Pattern = "(一是|二是|三是|四是|五是|六是)([^。])*。"
    Set s_titles = s_reg.Execute(strA)
    t2 = 1
    End If

    For Each s_title In s_titles
      With Selection.Find
    .ClearFormatting
    .Text = s_title.Value
    Debug.Print "二级标题遍历项目:"; s_title.Value
    .Execute Forward:=True
    End With

    With Selection.Font
            .Name = "楷体"
            .Size = "16"
            .ColorIndex = wdBlack
            .Bold = True
            End With
    Selection.HomeKey unit:=wdStory
    Next
    End If

    ' 以下是设置三级标题
    If t2 = 0 Then
    Selection.HomeKey unit:=wdStory
    th_reg.Global = True
    th_reg.Pattern = "\r\d{1,2}\.([^。])*。"
    Set th_titles = th_reg.Execute(strA)

    If th_titles.Count = 0 Then '如果三级标题是一是二是三是,则匹配
    th_reg.Pattern = "(一是|二是|三是|四是|五是|六是)([^。])*。"
    Set th_titles = th_reg.Execute(strA)
    End If
      
    For Each th_title In th_titles
      With Selection.Find
    .ClearFormatting
    .Text = th_title.Value
    Debug.Print "三级标题遍历项目:"; th_title.Value
    .Execute Forward:=True
    End With

    With Selection.Font
            .Bold = True
            .ColorIndex = wdBlack
            End With
    Selection.HomeKey unit:=wdStory
    Next

    End If
    End Sub


    Sub 页码设置()
    ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).PageNumbers.Add PageNumberAlignment:=wdAlignPageNumberCenter, FirstPage:=True

    With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary) '进入页脚编辑状态
    .Range.Font.Size = 15
    .Range.Font.Name = "仿宋"
    .Range.Collapse Direction:=wdCollapseEnd
    End With

    End Sub
    Sub 删除页眉横线()
    With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range '进入页脚编辑状态
    .Delete '删除页眉中的内容
    .ParagraphFormat.Borders(wdBorderBottom).LineStyle = wdLineStyleNone '段落下边框线
    End With
    End Sub
    Sub 公文格式排版()
    Call 文档初始化
    Call 标题正文设置
    Call 页码设置
    Call 删除页眉横线
    End Sub

    TA的精华主题

    TA的得分主题

    发表于 2021-5-14 17:08 | 显示全部楼层
    本帖最后由 413191246se 于 2021-5-14 20:20 编辑

    楼主,你的代码我在网上看到了。。。

    TA的精华主题

    TA的得分主题

    发表于 2021-6-1 13:51 | 显示全部楼层
    458863601 发表于 2021-5-14 08:49
    Sub 文档初始化() '公文格式初始化
    Selection.WholeStory '选择word 所有文档
    Selection.ClearFormatti ...

    这个可以批量把文件夹内几百个文件都修改了么
    您需要登录后才可以回帖 登录 | 免费注册

    本版积分规则

    关闭

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

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

    GMT+8, 2024-4-20 04:06 , Processed in 0.040606 second(s), 12 queries , Gzip On, MemCache On.

    Powered by Discuz! X3.4

    © 1999-2023 Wooffice Inc.

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

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

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