[Visual Basic] 纯文本查看 复制代码
Const TName = "CrackNoteTool"
Sub AutoExec() '启动Word或者加载全局模板时触发,添加自定义的工具栏
With Application.CommandBars.Add(Name:=TName, _
Position:=msoBarTop, Temporary:=True)
.Visible = True
With .Controls.Add(Type:=msoControlButton, ID _
:=1, Before:=1, Temporary:=True) 'ID要设置为1,表示自定义,和Excel里设置2950为自定义不同,要出错
.BeginGroup = False
.OnAction = "Page_Setup"
.FaceId = 501
.Caption = "Page setup"
End With
With .Controls.Add(Type:=msoControlButton, ID _
:=1, Before:=1, Temporary:=True) 'ID要设置为1,表示自定义,和Excel里设置2950为自定义不同,要出错
.BeginGroup = False
.OnAction = "Align_Columns"
.FaceId = 269
.Caption = "Align columns"
End With
End With
End Sub
Sub AutoExit() '退出Word或者卸载全局模板时触发
CommandBars(TName).Delete
End Sub
Sub Page_Setup() '页面设置
With ActiveDocument.PageSetup
.TopMargin = CentimetersToPoints(1)
.BottomMargin = CentimetersToPoints(1)
.LeftMargin = CentimetersToPoints(1)
.RightMargin = CentimetersToPoints(1)
End With
End Sub
Sub Align_Columns()
Dim s As String, tmp As String
Dim iStart As Long, iEnd As Long, pos As Long, i As Long
Dim reg As New RegExp
Dim m As MatchCollection
Const COL1 = 91.5, COL2 = 175.5, COL3 = 345.5 '各列的位置
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
'OD里复制下来的文本默认是左右对齐,这会使文本分得很散,格式不好控制,所以设置为左对齐
While Right(Selection, 1) = vbCr Or Right(Selection, 1) = vbLf '去掉选择末尾的空行
Selection.End = Selection.End - 1
Wend
iStart = Selection.Start
'iEnd = Selection.End '原来想用这个定位结尾,但是因为整理过程中内容经常会被替换,所以结束位置随时会变化
Selection.MoveRight unit:=wdCharacter, Count:=1 '移动光标到末尾
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="endSelection" '加个书签方便定位,这个防止操作处理过界
Selection.HomeKey unit:=wdStory '回到文档开始
Selection.MoveRight unit:=wdCharacter, Count:=iStart '回到选择开始
While Selection.Start < ActiveDocument.Bookmarks("endSelection").Start
'处理第一个Tab
Selection.MoveRight unit:=wdWord, Extend:=1
Selection = Trim(Selection)
Selection.MoveRight unit:=wdCharacter, Count:=1
Selection = vbTab
Selection.MoveRight unit:=wdCharacter, Count:=1
'处理第2个Tab
Selection.EndKey unit:=wdLine, Extend:=wdExtend '选择到行尾
s = Selection
reg.Pattern = "[a-z]{2,}\s?" '匹配汇编代码,前面的二进制代码里也会出现字母,但都是用大写字母,所以不会混淆
Set m = reg.Execute(s)
tmp = m.Item(0).Value
pos = InStr(s, tmp) - 1
Selection.End = Selection.Start + pos '这里的选择就是二进制代码
Selection = Trim(Selection)
Selection.MoveRight unit:=wdCharacter, Count:=1
If Selection.Information(wdHorizontalPositionRelativeToPage) >= COL2 Then
Selection = " "
Selection.MoveRight unit:=wdCharacter, Count:=1
Else
While Selection.Information(wdHorizontalPositionRelativeToPage) < COL2
Selection = vbTab
Selection.MoveRight unit:=wdCharacter, Count:=1
Wend
End If
'处理第3个Tab
Selection.EndKey unit:=wdLine, Extend:=wdExtend '选择到行尾
s = Selection
pos = InStr(s, ";")
If pos = 0 Then
Selection.MoveRight unit:=wdCharacter, Count:=1 '没有注释部分,后面都是汇编码
Else
Selection.End = Selection.Start + pos - 1 '这里选择的是汇编代码部分
Selection = Trim(Selection)
Selection.MoveRight unit:=wdCharacter, Count:=1
If Selection.Information(wdHorizontalPositionRelativeToPage) >= COL3 Then
Selection = " "
Selection.MoveRight unit:=wdCharacter, Count:=1
Else
While Selection.Information(wdHorizontalPositionRelativeToPage) < COL3
Selection = vbTab
Selection.MoveRight unit:=wdCharacter, Count:=1
Wend
End If
Selection.EndKey unit:=wdLine, Extend:=wdExtend
s = Selection
If Right(s, 1) = vbCr Then '防止太长的行有换行
Selection.MoveRight unit:=wdCharacter, Count:=1
Else
Selection.MoveRight unit:=wdCharacter, Count:=1
Selection.EndKey unit:=wdLine, Extend:=wdExtend
Selection.MoveRight unit:=wdCharacter, Count:=1
End If
End If
Wend
ActiveDocument.Bookmarks("endSelection").Delete '删除用于临时定位的书签
End Sub