我想在 MS Word 中创建的技术文档中包含 XML 架构。其中的行比页面的可用宽度长得多,因此 Word 会自动换行。我想让读者清楚地知道这种情况发生在哪里,这样他们就知道下一行是上一行的延续。有没有办法让 Word 在每行自动换行的末尾自动插入一个符号,例如 ⏎?
我当然可以手动完成,但每次架构更改时我都必须重复大量工作,而且容易出错。我知道“显示非打印字符”模式,但我希望这些符号只在此部分可见,并且也可以打印。此外,“显示非打印字符”显示明确的换行符而不是自动换行符。
使用 MS Word 可以实现这一点吗?如果不行,有人可以推荐另一种自动化技术来实现它吗?
答案1
好吧,您可以尝试以下 VBA 作为起点,但我认为它需要一些工作。它假设您的换行符始终位于空格之后(在我看来,这对于架构来说并非不合理),因此如果您的无空格文本长度超过一行,您可能仍需要手动换行。
这会将最后的空格替换为当前字体中的回车符 + 无宽度的断行空格,因此只要您可以调整回车符的宽度(有多种可行的方法可以做到这一点)以便 Word 仍然在相同的点换行,那就足够了。
Sub markAutoLineBreaks()
' Changes line breaks automatically made by Word
' into "return" charaters, but only where the line
' ends in a " "
' This operates on the text in the current selection
' We use a character style
Const strStyleName As String = "contchar"
Dim r As Word.Range
Dim styContchar As Word.Style
' Add the style if it is not present
On Error Resume Next
Set styContchar = ActiveDocument.Styles.Add(strStyleName, Type:=WdStyleType.wdStyleTypeCharacter)
Err.Clear
On Error GoTo 0
' Set the characteristics of the style. What you need to aim for
' is to adjust the character width so that the text breaks at the
' same point (if possible)
Set styContchar = ActiveDocument.Styles(strStyleName)
With styContchar.Font
.Size = 8
End With
' Save the selection
Set r = Selection.Range
' remove old line end marks
With Selection.Find
.ClearFormatting
.Style = styContchar
.Replacement.ClearFormatting
' Not sure what to use here, but this will have to do
.Replacement.Style = ActiveDocument.Styles("Default Paragraph Font")
' 9166 is the return character. 8204 is a No-width breaking space
.Text = ChrW(9166) & ChrW(8204)
.Replacement.Text = " "
.Forward = True
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Moving down lines is not completely straightforward
' but this seems to work
Selection.Collapse direction:=wdCollapseStart
Do Until Selection.End > r.End
Selection.Bookmarks("\line").Select
If Right(Selection, 1) = " " Then
Selection.SetRange Selection.End - 1, Selection.End
Selection.Delete
Selection.Text = ChrW(9166) & ChrW(8204)
Selection.Style = styContchar
Selection.Bookmarks("\line").Select
Selection.Collapse direction:=wdCollapseStart
End If
Selection.MoveDown wdLine, 1, False
Loop
' reselect our original selection
r.Select
Set r = Nothing
End Sub
答案2
我不会将其作为重复,因为这不是一个已回答的问题,但可能需要在这里查看:
是否可以设置样式来显示 Microsoft Word 中的行连续性?
这将引导你: 将 HTML 转换为图像