【转】给word中的代码着色

基本操作

1)用Notepad++直接编辑代码文件,注意文件后缀,比如.cpp是C++程序,.m是Matlab,linux文件是.sh,写对后缀表示的文件类型,才有对应的语法高亮效果。

2)选中需要的代码块(或者不选,默认对全文操作),使用“插件 -> NppExport”,具体见下图

3)直接粘贴到Word就行了。

更加智能与自动化

office套件提供了VBA,让用户可以写程序、宏来辅助完成办公操作。VBA使用最广泛的当属Excel了。Alt + F11可以进入VBA,然后点击“插入”-“模块”:

然后把下面的代码复制到“模块1”并保存。

Sub 设置代码表格()
' author: code4101
' 设置代码表格 宏
'
'
    ' 背景色为morning的配色方案,RGB为(229,229,229)
    With Selection.Tables(1)
        With .Shading
            .Texture = wdTextureNone
            .ForegroundPatternColor = wdColorAutomatic
            .BackgroundPatternColor = 15066597
        End With
        .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
        .AutoFitBehavior (wdAutoFitContent)  '自动调整大小
    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 = 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 输入连续数字()
' author: code4101
    行数 = InputBox("请输入代码终止行数", "输入行数", "50")
    For i = 1 To 行数 - 1
        Selection.TypeText Text:=i
        Selection.TypeParagraph
    Next
    Selection.TypeText Text:=行数
End Sub
这里的两个宏,一个是输入连续数字的宏,另一个是对代码表格进行一些处理的宏,选中那个一行两列的表格,跑跑宏就知道效果了。(这两个宏的配置会比较个性化,读者可以根据自己的喜好来开发。)
       代码的最终形式如下
 

参考:https://blog.csdn.net/code4101/article/details/41802715

 
 
 
 
 
 
 
 
 
 
posted @ 2018-11-12 15:16  意如柳  阅读(2049)  评论(0编辑  收藏  举报