手机
当前位置:查字典教程网 >软件教程 >办公软件 >Word中怎么设置代码高亮?word中关键字高亮的设置方法
Word中怎么设置代码高亮?word中关键字高亮的设置方法
摘要:有时写文档时需要将代码粘贴到word中,但直接粘贴到word中的代码虽能保持换行与缩进等格式,但在一般代码编辑工具中的关键字高亮功能却无法实...

有时写文档时需要将代码粘贴到word中,但直接粘贴到word中的代码虽能保持换行与缩进等格式,但在一般代码编辑工具中的关键字高亮功能却无法实现。该方法无需任何插件,只需要制作一个宏即可实现类似sublime的显示效果

Word中怎么设置代码高亮?word中关键字高亮的设置方法1

1、首先完成当前所有内容,建议把代码放入单独的文本框中,方便处理,也有较好的视觉效果。记得一定要拷贝一份副本!因为很可能因为代码量比较多,处理宏的时候卡死,只能强制退出WORD了。

2、当前文档新定义一个样式,命名为"code",专门用来对代码进行格式化。由于是代码,所以推荐中文使用黑体(注释等),而英文使用等宽字体(courier new)。步骤如图。

Word中怎么设置代码高亮?word中关键字高亮的设置方法2

3、选中代码,单击样式库 ccode,将代码应用该样式

Word中怎么设置代码高亮?word中关键字高亮的设置方法3

4、新建宏,步骤如图

Word中怎么设置代码高亮?word中关键字高亮的设置方法4

5、将VBA代码(在下一步中)拷贝进去,保存后关闭 (有VBA或相关程序经验者可根据自己需要进行相关修改,如关键词和高亮颜色等)

Word中怎么设置代码高亮?word中关键字高亮的设置方法5

复制内容到剪贴板 'scripttohighlightcodeIndocument PrivateFunctionisKeyword(w)AsBoolean DimkeysAsNewCollection Withkeys .Add"if":.Add"else":.Add"elseif":.Add"case":.Add"switch":.Add"break" .Add"for":.Add"continue":.Add"do":.Add"while":.Add"foreach":.Add"echo" .Add"define":.Add"array":.Add"NULL":.Add"function":.Add"include":.Add"return" .Add"global":.Add"as":.Add"die":.Add"header":.Add"this":.Add"empty" .Add"isset":.Add"mysql_fetch_assoc":.Add"class":.Add"style" .Add"name":.Add"value":.Add"type":.Add"width":.Add"_POST":.Add"_GET" EndWith isKeyword=isSpecial(w,keys) EndFunction PrivateFunctionisSpecial(ByValwAsString,ByRefcolAsCollection)AsBoolean ForEachiIncol Ifw=iThen isSpecial=True ExitFunction EndIf Next isspeical=False EndFunction PrivateFunctionisOperator(w)AsBoolean DimopsAsNewCollection Withops .Add"+":.Add"-":.Add"*":.Add"/":.Add"&":.Add"^":.Add";" .Add"%":.Add"#":.Add"!":.Add":":.Add",":.Add"." .Add"||":.Add"&&":.Add"|":.Add"=":.Add"++":.Add"--" .Add"'":.Add"""" EndWith isOperator=isSpecial(w,ops) EndFunction PrivateFunctionisType(ByValwAsString)AsBoolean DimtypesAsNewCollection Withtypes .Add"SELECT":.Add"FROM":.Add"WHERE":.Add"INSERT":.Add"INTO":.Add"VALUES":.Add"ORDER" .Add"BY":.Add"LIMIT":.Add"ASC":.Add"DESC":.Add"UPDATE":.Add"DELETE":.Add"COUNT" .Add"html":.Add"head":.Add"title":.Add"body":.Add"p":.Add"h1":.Add"h2" .Add"h3":.Add"center":.Add"ul":.Add"ol":.Add"li":.Add"a" .Add"input":.Add"form":.Add"b" EndWith isType=isSpecial(w,types) EndFunction SubSyntaxHighlight() DimwordCountAsInteger DimdAsInteger 'setthestyleofselection Selection.Style="ccode" d=0 wordCount=Selection.Words.Count Selection.StartOfwdWord Whiled<wordCount d=d+Selection.MoveRight(wdWord,1,wdExtend) w=Selection.Text IfisKeyword(Trim(w))=TrueThen Selection.Font.Color=wdColorBlue ElseIfisType(Trim(w))=TrueThen Selection.Font.Color=wdColorDarkRed Selection.Font.Bold=True ElseIfisOperator(Trim(w))=TrueThen Selection.Font.Color=wdColorBrown ElseIfTrim(w)="//"Then 'lInecomment Selection.MoveEndwdLine,1 commentWords=Selection.Words.Count d=d+commentWords Selection.Font.Color=wdColorGreen Selection.MoveStartwdWord,commentWords ElseIfTrim(w)="/*"Then 'blockcomment WhileSelection.Characters.Last<>"/" Selection.MoveLeftwdCharacter,1,wdExtend Selection.MoveEndUntil("*") Selection.MoveRightwdCharacter,2,wdExtend Wend commentWords=Selection.Words.Count d=d+commentWords Selection.Font.Color=wdColorGreen Selection.MoveStartwdWord,commentWords EndIf 'movethestartofselectiontonextword Selection.MoveStartwdWord Wend 'prepareForsetlInenumber Selection.MoveLeftwdWord,wordCount,wdExtend SetLIneNumber EndSub PrivateSubSetLIneNumber() DimlinesAsInteger lines=Selection.Paragraphs.Count Selection.StartOfwdParagraph Forl=1Tolines lIneNum=l&"" Ifl<10Then lIneNum=lIneNum&"" EndIf Selection.Text=lIneNum Selection.Font.Bold=False Selection.Font.Color=wdColorAutomatic p=Selection.MoveDown(wdLine,1,wdMove) Selection.StartOfwdLine Next EndSub

6、选定代码文本,然后执行highlight脚本:“视图”-“宏”- 选中“SyntaxHighlight”-“运行”,然后执行就可以了。处理较长代码时需要一定时间,请耐心等待。

Word中怎么设置代码高亮?word中关键字高亮的设置方法6

注意事项:处理前一定要对原文章进行备份,以免前功尽弃。处理较长代码时需要一定时间,请耐心等待。

【Word中怎么设置代码高亮?word中关键字高亮的设置方法】相关文章:

在Word中打钩和打叉的三种常用方法

在Word2010的编辑器中如何设置包含上下标的公式

如何设置word中的页边距

Word2007文档中的书签的使用方法

Word2003文档设置密码的方法

word中查看字数信息的方法

Word转成网页,网页转Word的方法

在Word文档中页面设置B4丢了的解决方法

在Word2007中显示文档结构图的设置方法是什么

WPS 2012表格设置二级下拉列表的方法

精品推荐
分类导航