Word允许您向文档中添加行号。这就是说的意思—文档中的每一行都从头到尾编号。在创建特殊文档(例如法律文档)或打印硬拷贝以逐行参考时,这将非常有用。

当您在打开连续行号的情况下打印文档时,Word将从开始到结束逐行编号。但是,如果仅打印选区(进行选择,显示“打印”对话框,并确保将“选区”指定为要打印的内容),则Word无法正确打印行号。 Word将所选内容中的行编号为整个文档,从一个开始,一直进行到所选内容。有人认为,一种更好的方法是让Word根据所选内容在文档中真正代表的内容打印行号。例如,如果您选择第57至72行作为选择并进行打印,则Word应该在打印输出的左侧而不是1至15上打印数字57至72。

不幸的是,没有简单的方法可以解决此问题。一种解决方法是不打印选择。 (此建议听起来可能比实际的要轻率得多。)而是打印页面。在“打印”对话框中,指定要打印的页面范围,而不是选择范围。 Word保持行号正确,就好像您是从文档开头开始进行编号一样。

另一种选择是依靠宏为您完成“肮脏的工作”。您可以使用宏来重置用于选择的起始行号。以下是一个简单的宏,它要求您提供起始行号,然后使用该行号作为左侧打印的第一个数字来打印您的选择。 (请注意,此宏不会以您指定的行号开始打印,而是使用您指定的数字作为所选内容的起始行号。)

Sub LineNumbersPrint()

Dim LineNumberStart As Integer     On Error GoTo GetOut

LineNumberStart = InputBox("First line number for printout?", _       "Line Numbers Printout")



With ActiveDocument.PageSetup         With .LineNumbering             .Active = True             .StartingNumber = LineNumberStart         End With     End With

ActiveDocument.PrintOut , Range:=wdPrintSelection

With ActiveDocument.PageSetup         With .LineNumbering             .Active = True             .StartingNumber = 1         End With     End With

GetOut:

End Sub

使用此宏假定您在实际打印之前查找所选内容的起始行号。可以使用Word的“打印预览”功能来完成此操作,但是一段时间后可能会感到厌烦。如果您进行了大量选择打印,那么您将更喜欢以下宏。它更复杂,但是它会自动确定选择开始时要使用的正确行号,然后打印选择。

Sub Correct_Line_Numbers()

Dim myRng As Range     Dim StartRng As Range     Dim iCount As Integer

'if you include the paragraph mark in your selection, then Word 'prints the subsequent line number; not the entire line, just the 'line number; therefore, if the last character of the current 'selection is a paragraph mark, then move the end position of 'the selection to the left by one character     If Selection.Characters.Last = Chr(13) Then         Selection.MoveEnd Count:=-1     End If

'set the current selection to a variable     Set myRng = Selection.Range

'set the start of the document to a variable     Set StartRng = ActiveDocument.Paragraphs(1).Range

With Selection         'go to the beginning of the line for the current selection and         'set the iCount variable so that it counts the current line         .HomeKey unit:=wdLine         iCount = 1

'if the cursor is not at the beginning of the document         'then move the cursor up by one line         'increment iCount by one each time the cursor is not at         'the beginning of the document         While Not Selection.InRange(StartRng)

.MoveUp unit:=wdLine             iCount = iCount + 1             'if the cursor is in a table, then the macro should             'reduce iCount; Word counts an entire table as one line             If Selection.Rows.Count > 0 Then                 iCount = iCount - 1             End If         Wend     End With

'reset the starting line number so that it equals the     'number of times the cursor was moved up by a line     ActiveDocument.PageSetup.LineNumbering.StartingNumber = iCount

'reselect the original selection     myRng.Select

'print out only the original selection     ActiveDocument.PrintOut Range:=wdPrintSelection

'reset the line number(by "undoing" the last two actions     '[fields update and change line number])

'so that line numbering begins at one     ActiveDocument.Undo     ActiveDocument.Undo

'reselect the original selection     myRng.Select End Sub

这个宏有一个警告。如果您的文档中有隐藏的文本,并且显示了该隐藏的文本,但未将其设置为打印出来,则此宏仍会像打印这些行一样对这些文本行进行计数。换句话说,如果您将隐藏文本显示在屏幕上,则该宏假定您正在打印隐藏文本。

注意:

如果您想知道如何使用此页面(或_WordTips_网站上的任何其他页面)中描述的宏,我准备了一个包含有用信息的特殊页面。

链接:/ wordribbon-WordTipsMacros [点击此处在新的浏览器标签中打开该特殊页面]。

_WordTips_是您进行经济有效的Microsoft Word培训的来源。

(Microsoft Word是世界上最流行的文字处理软件。)本技巧(852​​0)适用于Microsoft Word 2007、2010和2013。您可以在此处找到适用于Word的较早菜单界面的本技巧的版本:

链接:/ word-Correct_Line_Numbers_When_Printing_Selections [打印选择时更正行号]。