现在的位置: 首页 > 综合 > 正文

【转载】word中实现代码高亮的宏

2012年10月07日 ⁄ 综合 ⁄ 共 3039字 ⁄ 字号 评论关闭
这个宏的原作者不得知,转载留作备份。
  1. Sub SyntaxHighlight()
  2. '
  3. ' SyntaxHighlight Macro
  4. ' 宏在 2003-4-24 由 yu 创建
  5. ' 修改 2008-12-08 由 wt.yu
  6. '
  7. 'Customization goes here
  8. Dim keywords, sysclasses
  9. keywords = Array("abstract""asm""auto""bool""boolean""break""byte""case""cast""catch""char", _
  10. "class""const""continue""default""delete""do""double""dynamic_case""else""enum""explicit""export""extern""extends""false""final", _
  11. "finally""friend""float""for""goto""if""inline""implements""import""instanceof""inner""int", _
  12. "interface""long""native""new""null""operator""package""private""protected""public""return", _
  13. "short""sigend""static""static_cast""struct""super""switch""synchronized""template""this""throw""throws""transient""true", _
  14. "try""typedef""unsigned""union""using""virtual""void""volatile""while""include""std")
  15. sysclasses = Array("System""String""StringBuffer""Runnable""Thread""Exception""IOException""cout""cin""std""endl""vector")
  16. MFCclasses = Array("CWnd""HWND""CRect""HDC""CDialog""BOOL""TRUE")
  17. b = Selection.Start
  18. E = Selection.End
  19. tstr = Selection.Text
  20. ll = Len(tstr)
  21. pos = 1
  22. c = ""
  23. sb = 0
  24. se = 0
  25. While (pos < ll)
  26.     c = Mid(tstr, pos, 1)
  27.     Select Case c
  28.     Case "/"
  29.         If Mid(tstr, pos + 1, 1) = "/" Then 'Comments
  30.             sb = pos
  31.             se = InStr(pos, tstr, vbCr)
  32.             Selection.Start = b + sb - 1
  33.             Selection.End = b + se - 1
  34.             Selection.Font.Italic = True
  35.             Selection.Font.Color = wdColorTeal
  36.             pos = se + 1
  37.         Else
  38.             pos = pos + 1
  39.         End If
  40.     Case """"
  41.         sb = pos
  42.         se = InStr(pos + 1, tstr, """")
  43.         Selection.Start = b + sb - 1
  44.         Selection.End = b + se
  45.         Selection.Font.Color = wdColorGray45
  46.         pos = se + 1
  47.     Case "a" To "z""A" To "Z"
  48.         sb = pos
  49.         While (c <> " "And (c <> "("And (c <> ")"And (c <> "{"And (c <> "}"And (c <> vbCr) _
  50.         And (c <> "."And (c <> ";")
  51.             pos = pos + 1
  52.             c = Mid(tstr, pos, 1)
  53.         Wend
  54.         w = Mid(tstr, sb, pos - sb)
  55.         For i = 0 To UBound(keywords)
  56.             If w = keywords(i) Then
  57.                 Selection.Start = b + sb - 1
  58.                 Selection.End = b + sb + Len(w) - 1
  59.                 Selection.Font.Bold = True
  60.                 Selection.Font.Color = wdColorBlue
  61.             End If
  62.         Next
  63.         
  64.         For i = 0 To UBound(sysclasses)
  65.             If w = sysclasses(i) Then
  66.                 Selection.Start = b + sb - 1
  67.                 Selection.End = b + sb + Len(w) - 1
  68.                 Selection.Font.Color = wdColorRed
  69.             End If
  70.         Next
  71.         
  72.         For i = 0 To UBound(MFCclasses)
  73.             If w = MFCclasses(i) Then
  74.                 Selection.Start = b + sb - 1
  75.                 Selection.End = b + sb + Len(w) - 1
  76.                 Selection.Font.Color = wdColorGreen
  77.             End If
  78.         Next
  79.         
  80.         pos = pos + 1
  81.     Case Else
  82.         pos = pos + 1
  83.     End Select
  84. Wend
  85. End Sub

抱歉!评论已关闭.