|
* 功能:请执行《简易编号热键》宏,提示热键已经设置,在表格中选定任意一个单元格,按 F3 键:原样复制/F4 键:自动编号(向下编号至当前表格末行,编号格式有两种:纯数字/类似ZD2015037这样的<前缀+数字>。)
- Sub 简易编号热键()
- KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyF3), KeyCategory:=wdKeyCategoryMacro, Command:="原样复制"
- KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyF4), KeyCategory:=wdKeyCategoryMacro, Command:="自动编号"
- MsgBox "F3:原样复制 F4:自动编号", vbOKOnly + vbExclamation, "简易编号热键"
- End Sub
- Sub 原样复制()
- If Selection.Information(wdWithInTable) = False Then MsgBox "请将光标放在表格中!", vbOKOnly + vbCritical, "原样复制": End
- Dim m As Long, n As Long, h As Long, i As String, c As Cell
- m = Selection.Information(wdStartOfRangeRowNumber)
- n = Selection.Information(wdStartOfRangeColumnNumber)
- h = Selection.Tables(1).Rows.Count
- Selection.SelectCell: Selection.MoveEnd Unit:=wdCharacter, Count:=-1
- If Asc(Selection) = 13 Then End 'Empty
- i = Selection.Text
- ActiveDocument.Range(Start:=Selection.Tables(1).Cell(m + 1, n).Range.Start, End:=Selection.Tables(1).Cell(h, n).Range.End).Select
- For Each c In Selection.Cells
- c.Range.Text = i
- Next
- Selection.Tables(1).Cell(m, n).Select
- End Sub
- Sub 自动编号()
- If Selection.Information(wdWithInTable) = False Then MsgBox "请将光标放在表格中!", vbOKOnly + vbCritical, "自动编号": End
- Dim m As Long, n As Long, h As Long, i As String, j As String, k As Long, v As Long, c As Cell, s As String
- m = Selection.Information(wdStartOfRangeRowNumber)
- n = Selection.Information(wdStartOfRangeColumnNumber)
- h = Selection.Tables(1).Rows.Count
- Selection.SelectCell: Selection.MoveEnd Unit:=wdCharacter, Count:=-1
- If Asc(Selection) = 13 Then End 'Empty
- j = Selection.Text: k = Len(Selection)
- Selection.MoveStart Unit:=wdCharacter, Count:=k - 1
- If Selection Like "[!0-90-9]" Then Selection.EndKey Unit:=wdLine: End
- Do
- If Len(Selection) = k Then Exit Do
- Selection.MoveStart Unit:=wdCharacter, Count:=-1
- Loop Until Selection.Characters.First Like "[!0-90-9]"
- If Selection.Characters.First Like "[!0-90-9]" Then Selection.MoveStart Unit:=wdCharacter, Count:=1
- Selection.Range.CharacterWidth = wdWidthHalfWidth
- If Len(Selection) = k Then i = j: s = "" Else i = Selection.Text: s = Left(j, Len(j) - Len(i))
- i = i + 1
- ActiveDocument.Range(Start:=Selection.Tables(1).Cell(m + 1, n).Range.Start, End:=Selection.Tables(1).Cell(h, n).Range.End).Select
- For Each c In Selection.Cells
- c.Range.Text = s & i: i = i + 1
- Next
- Selection.Tables(1).Cell(m, n).Select
- End Sub
复制代码 |
|