非常感谢能够提供这个修改。我加入了跳过表格的功能。如果您觉得有必要处理可以加入这部分代码。
ElseIf outlinelevel = wdOutlineLevel6 Or outlinelevel = wdOutlineLevel7 Or outlinelevel = wdOutlineLevel8 Or outlinelevel = wdOutlineLevel9 Then
' 跳过6-9级的大纲级别处理 如果有一些文字不需要格式化可以设置为大纲级别6-9
' 第4、5级按正文格式处理,所以不跳过
ElseIf p.Range.Information(wdWithInTable) Then
' 跳过表格 表格一般比正文小一号、没有缩进
ElseIf p.Style.NameLocal = "题注" Then
' 表和图的标题的样式一般都是题注
https://getquicker.net/Sharedaction?code=8d8d6f34-a75d-45a4-d4bf-08db111f8bc2
这是修改后的动作,你看看有没有什么问题,如果功能一致且没有bug的话,我转为公开分享。
谢谢您采纳我的意见,
1、我的格式化正文时间很长,建议加入提示让用户等待,结束的时候关闭
2、格式化正文的全部代码。
这样写降低了屏幕刷新,增加了灵活性
$$Sub 总()
Dim sectionsToFormat As Variant
sectionsToFormat = Array({特定节}) ' 这里定义要处理的节号数组
Call FormatDocument(sectionsToFormat)
End Sub
Sub FormatDocument(sectionsToFormat As Variant)
Dim doc As Document
Set doc = ActiveDocument
Dim sec As Section
Dim p As Paragraph
Dim i As Integer
Dim mtable As table
Dim outlinelevel as WdOutlineLevel '这个变量频繁访问,
Application.ScreenUpdating = False '不刷新屏幕
For i = LBound(sectionsToFormat) To UBound(sectionsToFormat)
' 确保指定的节号在文档中存在
If sectionsToFormat(i) > doc.Sections.Count Or sectionsToFormat(i) <= 0 Then
MsgBox "节号 " & sectionsToFormat(i) & " 不存在。"
Exit Sub
End If
Set sec = doc.Sections(sectionsToFormat(i))
For Each p In sec.Range.Paragraphs
outlinelevel = p.OutlineLevel
If outlinelevel = wdOutlineLevel1 Then
With p.Range
.Font.NameFarEast = "{一级中文字体}"
.Font.NameAscii = "{一级西文字体}"
.Font.Size = {一级字号}
.Font.Bold = {一级加粗}
.Font.Italic = {一级倾斜}
.ParagraphFormat.CharacterUnitFirstLineIndent = {一级首行缩进}
.ParagraphFormat.SpaceBefore = {一级段前间距}
.ParagraphFormat.SpaceAfter = {一级段后间距}
.ParagraphFormat.Alignment = {一级对齐方式}
.ParagraphFormat.LineSpacingRule = {一级几倍行距}
If .ParagraphFormat.LineSpacingRule = wdLineSpaceAtLeast Then
.ParagraphFormat.LineSpacing = {一级行距}
End If
End With
ElseIf outlinelevel = wdOutlineLevel2 Then
With p.Range
.Font.NameFarEast = "{二级中文字体}"
.Font.NameAscii = "{二级西文字体}"
.Font.Size = {二级字号}
.Font.Bold = {二级加粗}
.Font.Italic = {二级倾斜}
.ParagraphFormat.CharacterUnitFirstLineIndent = {二级首行缩进}
.ParagraphFormat.SpaceBefore = {二级段前间距}
.ParagraphFormat.SpaceAfter = {二级段后间距}
.ParagraphFormat.Alignment = {二级对齐方式}
.ParagraphFormat.LineSpacingRule = {二级几倍行距}
If .ParagraphFormat.LineSpacingRule = wdLineSpaceAtLeast Then
.ParagraphFormat.LineSpacing = {二级行距}
End If
End With
ElseIf outlinelevel = wdOutlineLevel3 Then
With p.Range
.Font.NameFarEast = "{三级中文字体}"
.Font.NameAscii = "{三级西文字体}"
.Font.Size = {三级字号}
.Font.Bold = {三级加粗}
.Font.Italic = {三级倾斜}
.ParagraphFormat.CharacterUnitFirstLineIndent = {三级首行缩进}
.ParagraphFormat.SpaceBefore = {三级段前间距}
.ParagraphFormat.SpaceAfter = {三级段后间距}
.ParagraphFormat.Alignment = {三级对齐方式}
.ParagraphFormat.LineSpacingRule = {三级几倍行距}
If .ParagraphFormat.LineSpacingRule = wdLineSpaceAtLeast Then
.ParagraphFormat.LineSpacing = {三级行距}
End If
End With
ElseIf outlinelevel = wdOutlineLevel6 Or outlinelevel = wdOutlineLevel7 Or outlinelevel = wdOutlineLevel8 Or outlinelevel = wdOutlineLevel9 Then
' 跳过6-9级的大纲级别处理 如果有一些文字不需要格式化可以设置为大纲级别6-9
' 第4、5级按正文格式处理,所以不跳过
ElseIf p.Range.Information(wdWithInTable) Then
' 跳过表格 表格一般比正文小一号、没有缩进
ElseIf p.Style.NameLocal = "题注" Then
' 表和图的标题的样式一般都是题注
Else
With p.Range
.Font.NameFarEast = "{正文中文字体}"
.Font.NameAscii = "{正文西文字体}"
.Font.Size = {正文字号}
.Font.Bold = {正文加粗}
.Font.Italic = {正文倾斜}
.ParagraphFormat.CharacterUnitFirstLineIndent = {正文首行缩进}
.ParagraphFormat.SpaceBefore = {正文段前间距}
.ParagraphFormat.SpaceAfter = {正文段后间距}
.ParagraphFormat.Alignment = {正文对齐方式}
.ParagraphFormat.LineSpacingRule = {正文几倍行距}
If .ParagraphFormat.LineSpacingRule = wdLineSpaceAtLeast Then
.ParagraphFormat.LineSpacing = {正文行距}
End If
End With
End If
Next p
Application.ScreenRefresh ' 每一节完成更新一次屏幕
Next i
Application.ScreenUpdating = True '
End Sub
https://getquicker.net/Sharedaction?code=8d8d6f34-a75d-45a4-d4bf-08db111f8bc2
搞好了,试一下效果。你真的太厉害了,真是才注册quicker几天吗?还是说技术大佬用起来特别顺手,我学习官方文档好久还能做一些简单的动作,太佩服了,原谅我一开始以为你是小白,我才是小白