Word中設(shè)置代碼高亮?word中關(guān)鍵字高亮的設(shè)置圖文說明教程
發(fā)表時間:2023-09-05 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]有時寫文檔時需要將代碼粘貼到word中,但直接粘貼到word中的代碼雖能保持換行與縮進等格式,但在一般代碼編輯工具中的關(guān)鍵字高亮功能卻無法實現(xiàn)。該方法無需任何插件,只需要制作一個宏即可實現(xiàn)類似sublime的顯示效果 center">1、首先完成當(dāng)前所有內(nèi)容,建議把代碼放入單獨的文...
有時寫文檔時需要將代碼粘貼到word中,但直接粘貼到word中的代碼雖能保持換行與縮進等格式,但在一般代碼編輯工具中的關(guān)鍵字高亮功能卻無法實現(xiàn)。該方法無需任何插件,只需要制作一個宏即可實現(xiàn)類似sublime的顯示效果
1、首先完成當(dāng)前所有內(nèi)容,建議把代碼放入單獨的文本框中,方便處理,也有較好的視覺效果。記得一定要拷貝一份副本!因為很可能因為代碼量比較多,處理宏的時候卡死,只能強制退出WORD了。
2、當(dāng)前文檔新定義一個樣式,命名為"code",專門用來對代碼進行格式化。由于是代碼,所以推薦中文使用黑體(注釋等),而英文使用等寬字體(courier new)。步驟如圖。
3、選中代碼,單擊樣式庫 ccode,將代碼應(yīng)用該樣式
4、新建宏,步驟如圖
5、將VBA代碼(在下一步中)拷貝進去,保存后關(guān)閉 (有VBA或相關(guān)程序經(jīng)驗者可根據(jù)自己需要進行相關(guān)修改,如關(guān)鍵詞和高亮顏色等)
復(fù)制內(nèi)容到剪貼板
- 'script to high light code In document
-
- Private Function isKeyword(w) As Boolean
-
- Dim keys As New Collection
-
- With keys
-
- .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"
-
- End With
-
- isKeyword = isSpecial(w, keys)
-
- End Function
-
- Private Function isSpecial(ByVal w As String, ByRef col As Collection) As Boolean
-
- For Each i In col
-
- If w = i Then
-
- isSpecial = True
-
- Exit Function
-
- End If
-
- Next
-
- isspeical = False
-
- End Function
-
- Private Function isOperator(w) As Boolean
-
- Dim ops As New Collection
-
- With ops
-
- .Add "+": .Add "-": .Add "*": .Add "/": .Add "&": .Add "^": .Add ";"
-
- .Add "%": .Add "#": .Add "!": .Add ":": .Add ",": .Add "."
-
- .Add " ": .Add "&&": .Add " ": .Add "=": .Add "++": .Add "--"
-
- .Add "'": .Add """"
-
- End With
-
- isOperator = isSpecial(w, ops)
-
- End Function
-
- Private Function isType(ByVal w As String) As Boolean
-
- Dim types As New Collection
-
- With types
-
- .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"
-
- End With
-
- isType = isSpecial(w, types)
-
- End Function
-
- Sub SyntaxHighlight()
-
- Dim wordCount As Integer
-
- Dim d As Integer
-
- ' set the style of selection
-
- Selection.Style = "ccode"
-
- d = 0
-
- wordCount = Selection.Words.Count
-
- Selection.StartOf wdWord
-
- While d < wordCount
-
- d = d + Selection.MoveRight(wdWord, 1, wdExtend)
-
- w = Selection.Text
-
- If isKeyword(Trim(w)) = True Then
-
- Selection.Font.Color = wdColorBlue
-
- ElseIf isType(Trim(w)) = True Then
-
- Selection.Font.Color = wdColorDarkRed
-
- Selection.Font.Bold = True
-
- ElseIf isOperator(Trim(w)) = True Then
-
- Selection.Font.Color = wdColorBrown
-
- ElseIf Trim(w) = "//" Then
-
- 'lIne comment
-
- Selection.MoveEnd wdLine, 1
-
- commentWords = Selection.Words.Count
-
- d = d + commentWords
-
- Selection.Font.Color = wdColorGreen
-
- Selection.MoveStart wdWord, commentWords
-
- ElseIf Trim(w) = "/*" Then
-
- 'block comment
-
- While Selection.Characters.Last <> "/"
-
- Selection.MoveLeft wdCharacter, 1, wdExtend
-
- Selection.MoveEndUntil ("*")
-
- Selection.MoveRight wdCharacter, 2, wdExtend
-
- Wend
-
- commentWords = Selection.Words.Count
-
- d = d + commentWords
-
- Selection.Font.Color = wdColorGreen
-
- Selection.MoveStart wdWord, commentWords
-
- End If
-
- 'move the start of selection to next word
-
- Selection.MoveStart wdWord
-
- Wend
-
- ' prepare For set lIne number
-
- Selection.MoveLeft wdWord, wordCount, wdExtend
-
- SetLIneNumber
-
- End Sub
-
- Private Sub SetLIneNumber()
-
- Dim lines As Integer
-
- lines = Selection.Paragraphs.Count
-
- Selection.StartOf wdParagraph
-
- For l = 1 To lines
-
- lIneNum = l & " "
-
- If l < 10 Then
-
- lIneNum = lIneNum & " "
-
- End If
-
- Selection.Text = lIneNum
-
- Selection.Font.Bold = False
-
- Selection.Font.Color = wdColorAutomatic
-
- p = Selection.MoveDown(wdLine, 1, wdMove)
-
- Selection.StartOf wdLine
-
- Next
-
- End Sub
-
Office辦公軟件是辦公的第一選擇,這個地球人都知道。除了微軟Office,市面上也存在很多其他Office類軟件。