利用word的VBA,为代码统一表格宽度,底色及行号

如果文档中的代码表格时,感觉还是很快速有用的。

Sub HangHao()
'
' hanghao 宏
'
'
    Dim parag As Paragraph
    Dim nLineNum: nLineNum = 0
    Dim selRge As Range
    Set selRge = Selection.Range
    For Each parag In Selection.Paragraphs
        nLineNum = nLineNum + 1 bgNLineNum = nLineNum selRge.Paragraphs(nLineNum).Range.InsertBefore (Format$(bgNLineNum, "00") & " ") Next ' 背景色为morning的配色方案,RGB为(229,229,229) With Selection.Tables(1) With .Shading .Texture = wdTextureNone .ForegroundPatternColor = wdColorAutomatic .BackgroundPatternColor = 15066597 '15066597这个数其实就是229*(2^16 + 2^8 + 1) End With Options.DefaultHighlightColorIndex = wdNoHighlight 'NeXT_Jobs新增的:清除“以不同颜色突出显示文本”,从而解决从VS中“保留源格式”复制带来的文本高亮问题 Selection.Range.HighlightColorIndex = wdNoHighlight 'NeXT_Jobs新增的:清除“以不同颜色突出显示文本”,从而解决从VS中“保留源格式”复制带来的文本高亮问题 .Borders(wdBorderLeft).LineStyle = wdLineStyleNone .Borders(wdBorderRight).LineStyle = wdLineStyleNone .Borders(wdBorderTop).LineStyle = wdLineStyleNone .Borders(wdBorderBottom).LineStyle = wdLineStyleNone .Borders(wdBorderVertical).LineStyle = wdLineStyleNone .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone .Borders.Shadow = False End With With Options .DefaultBorderLineStyle = wdLineStyleSingle .DefaultBorderLineWidth = wdLineWidth050pt .DefaultBorderColor = wdColorAutomatic End With ' 段落无首行缩进,行间距为固定值12磅 With Selection.ParagraphFormat .LeftIndent = CentimetersToPoints(0) .RightIndent = CentimetersToPoints(0) .SpaceBefore = 0 .SpaceBeforeAuto = False .SpaceAfter = 0 .SpaceAfterAuto = False .LineSpacingRule = wdLineSpaceExactly '行距为“固定值” .LineSpacing = 16 '行距为“固定值”的“12磅” .KeepWithNext = False .KeepTogether = False .PageBreakBefore = False .NoLineNumber = False .Hyphenation = True '用连字符号连接 .FirstLineIndent = CentimetersToPoints(0) .OutlineLevel = wdOutlineLevelBodyText .CharacterUnitLeftIndent = 0 .CharacterUnitRightIndent = 0 .CharacterUnitFirstLineIndent = 0 .LineUnitBefore = 0 .LineUnitAfter = 0 .MirrorIndents = False .TextboxTightWrap = wdTightNone .AutoAdjustRightIndent = True .DisableLineHeightGrid = False .FarEastLineBreakControl = True .WordWrap = True .HangingPunctuation = True .HalfWidthPunctuationOnTopOfLine = False .AddSpaceBetweenFarEastAndAlpha = True .AddSpaceBetweenFarEastAndDigit = True .BaseLineAlignment = wdBaselineAlignAuto End With ' 清除原有的段落底纹 Selection.ParagraphFormat.Shading.BackgroundPatternColor = wdColorAutomatic End Sub Sub table_100() ' ' table_100 宏 ' ' Dim tempTable As Table Application.ScreenUpdating = False '判断文档是否被保护 If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then MsgBox "文档已保护,此时不能选中多个表格!" Exit Sub End If '删除所有可编辑的区域  ActiveDocument.DeleteAllEditableRanges wdEditorEveryone '添加可编辑区域 For Each tempTable In ActiveDocument.Tables tempTable.Range.Editors.Add wdEditorEveryone tempTable.PreferredWidthType = wdPreferredWidthPercent tempTable.PreferredWidth = 100 Next '选中所有可编辑区域  ActiveDocument.SelectAllEditableRanges wdEditorEveryone '删除所有可编辑的区域  ActiveDocument.DeleteAllEditableRanges wdEditorEveryone Application.ScreenUpdating = True End Sub

来个效果:

 

猜你喜欢

转载自www.cnblogs.com/aguncn/p/9966705.html
今日推荐