两个word宏(1)编号重排(2)给代码加上行号

类别:编程语言 点击:0 评论:0 推荐:
今天抽时间写了两个word的宏,用于排版挺有用的。一个用于编号重排,一个是给程序加上行号。

Sub 批量后退()
'
' 批量后退,这个宏用于将出版书籍时对编号(如图像编号、章节编号)进行整体调整
' 宏在 2005-1-30 由 陈刚 录制
'
Dim prefix As String
Dim startI As Integer
Dim endI As Integer
Dim spaceI As Integer

prefix = InputBox("前缀", "", "图9.")
startI = InputBox("开始")
endI = InputBox("结束", "", "100")
spaceI = InputBox("后退值", "", "1")

Set myRange = ActiveDocument.Content
For i = endI To startI Step -1
    myRange.Find.Execute FindText:=prefix & i, ReplaceWith:=prefix & (i + spaceI), Replace:=wdReplaceAll
Next i

End Sub





Sub 给程序加编号()
'
' 宏在 2005-1-30 由 陈刚 录制
' 给程序加编号,这个宏用于给选定区域的程序加上编号。
' 这个程序有几个特点:
' (1)在定位行之前要先定位页,因为word的行号是以页为基础的。
' (2)Information中只有开始字符所在行的信息,却没有结束行的信息(或者是我没有找到)。
' 这个程序有一个缺陷,由于没找到获得所选区域总行数的方法。所以选择区域必须为一页,
'不能跨页选择,否则出错
'

startNum = InputBox("输入开始编号", "", "1")
pageNum = Selection.Information(wdActiveEndAdjustedPageNumber) '得到当前页号
startLine = Selection.Information(wdFirstCharacterLineNumber) '得到第一行的行号
Selection.EndKey Unit:=wdLine '相当于按一下end键
endLine = Selection.Information(wdFirstCharacterLineNumber) '得到按end键后的行号,即所选区域的结束行号

Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=pageNum '定位页(光标会自动停在页的第一行)
Selection.MoveDown Unit:=wdLine, Count:=startLine - 1 '下移数行,到达所选区域的第一行

For i = 0 To endLine - startLine
    Selection.HomeKey Unit:=wdLine '相当于按钮Home键
    Selection.TypeText Text:="(" & i + startNum & ")  " '相当于敲入字符(1)等
    Selection.MoveDown Unit:=wdLine, Count:=1 '下移一行
Next i

End Sub

 


本文地址:http://com.8s8s.com/it/it23376.htm