自己写C#代码自动填充颜色VBA

文摘   2024-09-03 07:55   云南  

Sub sss()

'

' sss

'

'

Call FormatCSharpCode

End Sub

Sub FormatCSharpCode()

Dim doc As Document

Set doc = ActiveDocument

' Set the entire document to the default font and color

With doc.Content.Font

.Name = "Consolas"

.Size = 10

.color = wdColorAutomatic

End With

' Define arrays forkeywords, comments, and strings

Dim keywords As Variant

keywords = Array("using", "namespace", "class", "internal", "public", "private", "protected", _

"void", "static", "int", "string", "bool", "return", "if", "else", "foreach", _

"for", "while", "do", "switch", "case", "break", "continue", "try", "catch", _

"finally", "new", "async", "await", "Task", "List", "var", "this", "base", "null", "Exception", "uint", "ref")

Dim keyword As Variant

For Each keyword In keywords

Call ApplyColor(CStr(keyword), wdColorBlue)

Next keyword

keywords = Array("QueuedTask", "Button", "Map", "Layer", "FeatureLayer","FieldDescription", "MapPoint", _

"Polygon", "PolygonBuilderEx", "Math", "Geometry", "MessageBox", "RowCursor", "QueryFilter", "Feature", "Directory", _

"SpatialReferences","MapView", "CancelableProgressorSource","FeatureClass", "Polyline", "RowBuffer", "Project", "IGPResult", "Geoprocessing", _

"GPExecuteToolFlags","ProgressDialog")

For Each keyword In keywords

Call ApplyColor(CStr(keyword), RGB(43, 163, 213))

Next keyword

' Apply green color for comments

Call ApplyColor("//*", wdColorGreen, True)

' Apply red color forstrings

Call ApplyColor("""*", wdColorRed, True)

' Apply grey color forclass names after "class"

Call ApplyColor("class *", RGB(169, 169, 169), True)

' Optionally, apply other formatting rules as needed

End Sub

Sub ApplyColor(searchText As String, color As WdColor, Optional isWildcard As Boolean = False)

With Selection.Find

.ClearFormatting

.Replacement.ClearFormatting

.Text = searchText

If isWildcard Then

.MatchWildcards = True

Else

.MatchWholeWord = True

End If

.Replacement.Text = "^&"

.Replacement.Font.color = color

.Forward = True

.Wrap = wdFindContinue

.Format = True

.Execute Replace:=wdReplaceAll

End With

End Sub

GISAI
ArcGIS培训和二次开发
 最新文章