引言
学数学和计算机,当然还是用LaTeX排版技术文章更方便。但有时候还是迫不得已需要用Word写作,另外Word其实也有Word的好处,比如细节上的修改要比LaTeX方便。
从Matlab高亮代码复制到Word,中文会乱码开始,我就很想研究下如何在Word中展示漂亮的代码。今年寒假利用Vim,有些突破,10月3日的时候又有了比较大的进展,自己设计了一款Vim的代码高亮配色方案,然后利用Vim的:TOhtml命令生成html文件,再用浏览器打开html文件,复制里面的代码到Word,就能保留原始的高亮效果了。
其实本质就是制作一份html文件,复制到Word时会保留其源格式。很多代码编辑器或者IDE都有提供制作html文件的功能,比如CodeBlocks(File -> Export -> As HTML...)。后来我发现Notepad++才是最方便的工具。然后结合看Oeasy的Word教学视频,对底纹等排版知识有了更深的了解,加上一点VBA的功底,目前能比较高效的进行Word的代码排版工作了。
本打算VBA钻研的更深入些,把整个流程做的更加智能、自动化后再分享心得。不过该方法其实目前也比较完善成熟了,故提前完成了这篇博客并分享。
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
原文地址:http://blog.csdn.net/code4101/article/details/41802715