【转】给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
htmlborder.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 = 1To⾏数 - 1
Selection.TypeText Text:=i
Selection.TypeParagraph
Next
Selection.TypeText Text:=⾏数
End Sub
这⾥的两个宏,⼀个是输⼊连续数字的宏,另⼀个是对代码表格进⾏⼀些处理的宏,选中那个⼀⾏两列的表格,跑跑宏就知道效果了。(这两个宏的配置会⽐较个性化,读者可以根据⾃⼰的喜好来开发。)
代码的最终形式如下
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论