word中代码高亮加行号

word中代码高亮加行号

在IDE,网页中代码都是精心排版的,美观漂亮。但是我们在自己的文档中记录笔记写书籍的时候,word文档中的代码却不尽人意,所以通过查找资料,发现有大神已经早就解决好了word中代码排版的问题!
如下图的样式:
这里写图片描述

那么如何来实现word中的代码高亮加行号,这就是利用word复制粘贴富文本的功能实现代码高亮和VB宏编程来加行号。

第一步:安装notepad,将代码复制粘贴到notepad中,调整文本中的代码语言,使其格式化。

这里写图片描述

第二步:利用notepad中的插件复制富文本

这里写图片描述

第三步:word中插入2列一行表格,创建宏。宏代码如下

Sub 设置代码表格()

' author: code4101

' 设置代码表格 宏

'

'

 ' 背景色为morning的配色方案,RGB为(229,229,229)

 With Selection.Tables(1)

 With .Shading

 .Texture = wdTextureNone

 .ForegroundPatternColor = wdColorAutomatic

 .BackgroundPatternColor = 16514043

 End With

 .Borders(wdBorderLeft).LineStyle = wdLineStyleNone

 .Borders(wdBorderRight).LineStyle = wdLineStyleNone

 .Borders(wdBorderTop).LineStyle = wdLineStyleNone

 .Borders(wdBorderBottom).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 = 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

第四步:执行宏,格式化表格和加行号。

这里写图片描述

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值