利用VBA在Word中排出漂亮的代码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33308135/article/details/81201644

引言


在学习编程的过程中,常常会使用word来做笔记,下面我将对如何利用word宏来进行代码的排版进行说明


1.工具

  我用的是word2007,word2003和word2010操作也差不多

2.基本操作

  从visual studio、QT、Android studio复制代码到word时,通常都会自带高光的,直接复制粘贴到word就行了。
  但是作为一个精致的程序媛,怎么可能只满足这么点要求呢,我们通常看到的代码除了高光,还有灰色的背景和代码行号,下面才是我想介绍的重点了。
  话不多说,为了不耽搁各位时间,先上图,看完图感兴趣的就接着看,觉得没什么用的看完图就可以先撤退了。
图1

图1

图2
图2

  
  通常复制到word中的代码如图1所示,利用VBA进行word宏的编写可以为
代码添加行号和灰色背景

3.实现方法

  打开word,利用Alt +F11,可以进入VBA,然后在“Normal -> 模块”,右键选择“插入模块”就行了,然后进行代码编写。(Normal是Word打开时会自动载入的一个模板文件,把宏放在这里,可以保证在任意一个Word文档中都能调用。)

部分代码

 '统计行数
    Dim wordCount, lineCount
    Set myRange = Selection.Range
    lineCount = myRange.ComputeStatistics(Statistic:=wdStatisticLines) 
    '插入1*2的表格
    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, 

NumColumns:= _
        2, DefaultTableBehavior:=wdWord9TableBehavior, 

AutoFitBehavior:= _
        wdAutoFitFixed
    With Selection.Tables(1)
        If .Style <> "网格型" Then
            .Style = "网格型"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        .ApplyStyleRowBands = True
        .ApplyStyleColumnBands = False
    End With
    '设置代码行号   
    For i = 1 To lineCount - 1
        Selection.ParagraphFormat.LineSpacingRule = 

wdLineSpaceExactly
        Selection.ParagraphFormat.LineSpacing = 12
        Selection.Font.Size = 11
        Selection.Font.Color = Black
        Selection.Font.Name = Tahoma
        Selection.TypeText Text:=i
        Selection.TypeParagraph '换行              
    Next
    Selection.TypeText Text:=lineCount
   Selection.Tables(1).Select
      ' 背景色为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.Font.Size = 11
    Selection.Font.Name = Tahoma
    ' 清除原有的段落底纹
    Selection.ParagraphFormat.Shading.BackgroundPatternColor = 

wdColorAutomatic

  先选中需要进行处理的代码,然后点击word中开发工具,点击宏,运行即可。
  也可以通过word选项,自定义,添加宏快捷键,运行时只需要先选中代码,然后点击快捷键。
  详情可以去我上传的资源下载哦。
下载资源

猜你喜欢

转载自blog.csdn.net/qq_33308135/article/details/81201644