1. 程式人生 > >利用word的VBA,為程式碼統一表格寬度,底色及行號

利用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

來個效果: