如何在Word中排版出漂亮的编程语言代码样式?(较好用的部分方法汇总)

时间:2021-09-24 19:15:39


[1] 能输出各种编程语言并保持代码格式和语法高亮显示的网站:

PlanetB | Syntax Highlight Code in Word Documents  http://www.planetb.ca/syntax-highlight-word

Welcome! — Pygments  http://pygments.org/

[2] How do you display code snippets in MS Word preserving format and syntax highlighting?

https://*.com/questions/387453/how-do-you-display-code-snippets-in-ms-word-preserving-format-and-syntax-highlig#

[3] 如何在Word中排出漂亮的代码 - CSDN博客  http://blog.csdn.net/code4101/article/details/41802715

本人使用VS 2013才开始学习C语言,直接将代码从VS中“保留源格式”复制到Word中,因此在原作者写的宏的基础上加了两条宏命令拿来使用的,即:

Options.DefaultHighlightColorIndex = wdNoHighlight  'NeXT_Jobs新增的:清除“以不同颜色突出显示文本”,从而解决从VS中“保留源格式”复制带来的文本高亮问题
Selection.Range.HighlightColorIndex = wdNoHighlight  'NeXT_Jobs新增的:清除“以不同颜色突出显示文本”,从而解决从VS中“保留源格式”复制带来的文本高亮问题
Sub Word中代码排版之设置代码表格()
' author: code4101 链接http://blog.csdn.net/code4101/article/details/41802715
' Word中代码排版之设置代码表格 宏
'第一个宏需要选定一个一行两列的表格
'
    ' 背景色为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
        .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  '行距为“固定值”的“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 Word中代码排版之输入连续数字()
' author: code4101  链接http://blog.csdn.net/code4101/article/details/41802715
' Word中代码排版之设置代码表格 宏
'第二个宏是在表格第一列自动加上行号
'
    行数 = InputBox("请输入代码终止行数", "输入行数", "50")
    For i = 1 To 行数 - 1
        Selection.TypeText Text:=i
        Selection.TypeParagraph
    Next
    Selection.TypeText Text:=行数
End Sub

 如何在Word中排版出漂亮的编程语言代码样式?(较好用的部分方法汇总)

Office VBA 参考Office VBA Reference | MSDN  https://msdn.microsoft.com/vba/office-vba-reference

[4] Word里如何排版出漂亮的编程语言代码样式_百度经验  

https://jingyan.baidu.com/article/9c69d48f99eb1b13c8024e7d.html

[5] word2013种如何插入代码着色的代码块? - 知乎  https://www.zhihu.com/question/31544667

[6]